Reputation: 371
i'm using Delphi XE3 and below is my sample application:
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.
Under the Windows | Control Panel | Devices and Printers, there are 3 printers:
When i run the sample application and click the Button1, it shows "CutePDF Writer" as default printer. Without close the sample application, i go to Windows | Control Panel | Devices and Printers to set "My Fax" as default printer, then i go back to the sample application and click the Button1 again, it still shows "CutePDF Writer" as default printer (it should show "My Fax"). After studied the class TPrinter in unit Vcl.Printers, I can write the codes as below:
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Printer.Printing then
Printer.PrinterIndex := -1;
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;
It is not a good way for everytime require to set the PrinterIndex to -1. My question is how do my application know if there is default printer changes notification? so that i only set the PrinterIndex to -1 if there is default printer changes notification.
Upvotes: 0
Views: 4640
Reputation: 1
Just add : Printer.Refresh;
Like this :
procedure TForm1.Button1Click(Sender: TObject);
begin
Printer.Refresh;
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;
Upvotes: -1
Reputation: 613531
You can listen for WM_SETTINGCHANGE
notification messages. The MSDN documentation is a little sparse, but the sample code from the documentation of SetDefaultPrinter
makes it clear that a WM_SETTINGCHANGE
message should be broadcast by any party that modifies the default printer.
Unfortunately the WM_SETTINGCHANGE
does not include any information that allows you to determine whether or not the default printer has been changed. You've no way of knowing whether or not a particular WM_SETTINGCHANGE
message indicates change of default printer or indicates change of some other setting.
However, I would question your belief that you should be responding to this message. Consider the following scenario:
The thing is the application has a history. The last time the user printed they explicitly selected printer A. Why should a change to the default printer mean that next time round the application should offer the new default printer rather than the last printer that the user chose to use?
Upvotes: 2