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:
- CutePDF Writer (Default Printer)
- My Fax
- Microsoft XPS Document Writer
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.