“没有选择默认打印机"未设置Windows默认打印机时出现错误 [英] "There is no default printer selected" error when Windows default printer is not set up

查看:779
本文介绍了“没有选择默认打印机"未设置Windows默认打印机时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有Windows默认打印机设置的情况下,我需要打开打印机"对话框.设置Windows默认打印机时,可以使用以下代码正常工作.

I need to open the Printer Dialog box when there is no Windows default printer setup. It works fine with the below code when a Windows default printer is set up.

TPrintDialog *dlgPrint = new TPrintDialog(frmDisplayDetail);
if( dlgPrint->Execute()) { //code here }

但是,如果Windows中没有默认的打印机设置,则dlgPrint->Execute()会引发异常:

But if there is no default printer setup in Windows, dlgPrint->Execute() throws an exception:

未选择默认打印机

There is no default printer selected

要检查默认打印机索引,我使用了Printer()->PrinterIndex.没有Windows打印机设置时,无法访问此值:

To check the default printer index, I used Printer()->PrinterIndex. This value becomes inaccessible when there is no Windows printer setup:

错误E2122函数调用被地址XXX上未处理的异常XXX终止

error E2122 Function call terminated by unhandled exception XXX at address XXX

我做错什么了吗?请提出解决方案.

Am I doing something wrong? Please suggest a solution.

推荐答案

仅当TPrinter::SetToDefaultPrinter()方法出现以下情况时,才会抛出异常当前未选择默认打印机":

The exception "There is no default printer currently selected" is thrown only by the TPrinter::SetToDefaultPrinter() method, when either:

  • the Win32 API EnumPrinters() function fails with an ERROR_INVALID_NAME error code when enumerating with the (undocumented) PRINTER_ENUM_DEFAULT flag.

TPrinter::Printers列表不包含默认打印机.如果上面的EnumPrinters()没有报告默认设备,则从Win32 API GetDefaultPrinter() 函数(Delphi/C ++ Builder 2009及更高版本),或%windir%\win.ini(Delphi/C ++ Builder)[windows]部分的device值2007年及更早版本.)

the TPrinter::Printers list does not contain the default printer. If EnumPrinters() above does not report a default device, the default is then queried from the Win32 API GetDefaultPrinter() function (Delphi/C++Builder 2009 and later), or the device value of the [windows] section of %windir%\win.ini (Delphi/C++Builder 2007 and earlier).

TPrinter::SetToDefaultPrinter()仅由以下人员调用:

  • TPrinter::PrinterIndex吸气剂,如果FPrinterIndex成员当前为-1.

  • the TPrinter::PrinterIndex getter if the FPrinterIndex member is currently -1.

TPrinter::PrinterIndex设置器,如果FPrinterIndex成员当前为-1,或者属性设置为-1.

the TPrinter::PrinterIndex setter if the FPrinterIndex member is currently -1, or the property is being set to -1.

TPrintDialog::Execute()(使用Win32 API 函数)首先调用TPrinter::GetPrinter()以获取当前所选打印机的DEVMODE的句柄,然后将其用于初始化对话框(通过

TPrintDialog::Execute() (which uses the Win32 API PrintDlg() function) first calls TPrinter::GetPrinter() to get a handle to the DEVMODE of the currently selected printer, which it then uses to initialize the dialog (via the PRINTDLG::hDevMode field). TPrinter::GetPrinter() reads the TPrinter::PrinterIndex property, so if there is no currently selected printer and no default printer, the exception is thrown.

如果没有配置默认打印机,那么您基本上就走运了,因为您甚至无法将TPrinter::PrinterIndex属性设置为值> = 0,因为它会在检查新值之前首先调用SetToDefaultPrinter()匹配当前值.

If there is no default printer configured, you are basically out of luck, as you can't even set the TPrinter::PrinterIndex property to a value >= 0 since it will first call SetToDefaultPrinter() before checking if the new value matches the current value.

TPrinter的失败/崩溃历史由来已久.若要解决此问题,您应该改为直接调用Win32 API PrintDlg()函数.至少然后,您可以手动调用TPrinter::GetPrinter()(以获取初始的DEVMODE)并将其包装在try/catch块中,以丢弃它可能引发的任何异常.

TPrinter has a long history of failing/crashing when there is no default printer configured in Windows. To work around this, you should call the Win32 API PrintDlg() function directly instead. At least then, you can manually call TPrinter::GetPrinter() (to get the initial DEVMODE) and wrap it in a try/catch block to discard any exceptions that it may throw.

如果对话框结果指示成功,则可以手动调用TPrinter::SetPrinter(),将选定的打印机选项分配给TPrinter,以进行后续打印.

If the dialog result indicates success, you can manually call TPrinter::SetPrinter() to assign the selected printer options to TPrinter for subsequent printing.

这篇关于“没有选择默认打印机"未设置Windows默认打印机时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆