Delphi 我的应用程序如何知道是否有默认的打印机更改通知
我正在使用Delphi XE3,下面是我的示例应用程序:Delphi 我的应用程序如何知道是否有默认的打印机更改通知,delphi,delphi-xe3,printers,Delphi,Delphi Xe3,Printers,我正在使用Delphi XE3,下面是我的示例应用程序: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Butto
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
uses Vcl.Printers;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;
end.
在Windows |控制面板|设备和打印机下,有3台打印机:
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Printer.Printing then
Printer.PrinterIndex := -1;
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;
对于每次需要将PrinterIndex设置为-1的情况,这不是一个好方法。我的问题是,我的应用程序如何知道是否存在默认的打印机更改通知?因此,如果存在默认的打印机更改通知,我仅将PrinterIndex设置为-1。您可以侦听
WM\u SETTINGCHANGE
通知消息。MSDN文档有点稀疏,但的文档中的示例代码清楚地表明,WM_SETTINGCHANGE
消息应由修改默认打印机的任何一方广播
不幸的是,WM_SETTINGCHANGE
中没有任何信息可用于确定默认打印机是否已更改。您无法知道特定的WM_SETTINGCHANGE
消息是否表示默认打印机的更改或其他设置的更改
然而,我怀疑你是否认为你应该回应这个信息。考虑下面的场景:
- 你有一台有两台打印机的机器,a和B
- 打印机A是默认打印机
- 应用程序启动
- 用户打印,但选择打印机B
- 用户再次打印。程序记得上次用户想要打印机B,但这次用户想要打印机A,所以更改为该打印机
- 然后用户在控制面板中将默认打印机更改为打印机B
- 用户再次进入打印。应向用户提供哪种打印机
问题是应用程序有一个历史记录。用户上次打印时明确选择了打印机A。为什么更改默认打印机意味着下次应用程序应提供新的默认打印机,而不是用户选择使用的最后一台打印机?只需添加:printer.Refresh 像这样:
procedure TForm1.按钮1点击(发送方:TObject);
开始
打印机。刷新;
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
结束代码>什么?上面的场景与我的实际应用程序类似,用户打印报告,每个报告都有一个唯一的ID。每次用户打印报告时,应用程序都会从表中搜索,并根据用户名和报告ID返回最后的打印机名,如果没有找到最后的打印机名,则会返回默认的打印机名。完成报告打印后,应用程序将更新表。我使用unit Vcl.Printers中的Printer.Printers[Printer.PrinterIndex]返回默认打印机名称,不幸的是,我遇到了一个问题,即Printer.Printers[Printer.PrinterIndex]不返回最新的默认打印机名称如果用户在“控制面板”中更改默认打印机,我必须将printer.PrinterIndex设置为-1以通知打印机选择最新的默认打印机。printer.PrinterIndex]
不返回默认打印机名称。它返回所选打印机的名称。