如果用户的 PC 没有安装打印机,为啥 Delphi 7 应用程序会以“遇到问题并需要关闭”而退出?
Posted
技术标签:
【中文标题】如果用户的 PC 没有安装打印机,为啥 Delphi 7 应用程序会以“遇到问题并需要关闭”而退出?【英文标题】:Why might a Delphi 7 application exit with "encountered a problem and needs to close" if the user's PC does not have a printer installed?如果用户的 PC 没有安装打印机,为什么 Delphi 7 应用程序会以“遇到问题并需要关闭”而退出? 【发布时间】:2009-12-30 08:58:02 【问题描述】:我收到关于我的应用程序(使用 Delphi 7 编译)拒绝在某些 PC 上启动的零星报告 - 它立即退出并显示 Windows 错误消息:
“....exe 遇到问题并且 需要关闭。我们很抱歉 不便”
到目前为止,我的建议始终是安装默认打印机(任何打印机 - 甚至是 PDF 打印机),问题就消失了。如何修复应用程序?我的程序仅在用户按下 Print 按钮时访问 PrinterDialog 控件。
【问题讨论】:
【参考方案1】:我收到一封包含 bug report generated by MadExcept 的电子邮件,其中包含非常有用的堆栈跟踪:
exception class : EReadError
exception message : Error reading PrinterDialog.Copies: Operation not supported on selected printer.
main thread ($5d4):
0044c0a2 Classes HandleException
0044c258 Classes TReader.ReadProperty
0044bbf5 Classes TReader.ReadDataInner
0044bbd7 Classes TReader.ReadData
00450675 Classes TComponent.ReadState
0044ba51 Classes TReader.ReadComponent
0044bc69 Classes TReader.ReadDataInner
0044bba8 Classes TReader.ReadData
00450675 Classes TComponent.ReadState
004963a5 Controls TControl.ReadState
004998c9 Controls TWinControl.ReadState
004afcc9 Forms TCustomForm.ReadState
0044c9a9 Classes TReader.ReadRootComponent
00449f42 Classes TStream.ReadComponent
00446a3b Classes InternalReadComponentRes
00446bc5 Classes InitComponent
00446c56 Classes InitInheritedComponent
004af661 Forms TCustomForm.Create
004518b0 Classes StdWndProc
004b7111 Forms TApplication.CreateForm
006abe32 176 initialization
问题是由于我在设计时设置了 TPrinterDialog 的一些属性,例如打印页码的标志。当 Delphi 尝试在没有打印机的 PC 上创建表单并设置这些属性时,就会出现错误。
解决方案是删除旧的 TPrinterDialog 并用新的默认 TPrinterDialog 替换它,并在安装新打印机后在运行时设置属性。
【讨论】:
【参考方案2】:您可以“尝试除”崩溃语句:
Try
Statement
Statement...
Except
Statement
Statement...
End;
http://www.delphibasics.co.uk/RTL.asp?Name=Try try/except 的进一步说明
【讨论】:
以上是关于如果用户的 PC 没有安装打印机,为啥 Delphi 7 应用程序会以“遇到问题并需要关闭”而退出?的主要内容,如果未能解决你的问题,请参考以下文章