“没有选择默认打印机"未设置Windows默认打印机时出现错误

编程入门 行业动态 更新时间:2024-10-25 00:30:08
本文介绍了“没有选择默认打印机"未设置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:

  • Win32 API EnumPrinters() 函数失败,并显示ERROR_INVALID_NAME错误代码.

  • 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的句柄,然后将其用于初始化对话框(通过 PRINTDLG::hDevMode 字段). TPrinter::GetPrinter()读取TPrinter::PrinterIndex属性,因此,如果当前没有选择的打印机且没有默认打印机,则会引发异常.

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默认打印机时出现错误

本文发布于:2023-06-05 19:14:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/526482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机时   出现错误   打印机   Windows   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!