Pregunta

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:

  1. CutePDF Writer (Default Printer)
  2. My Fax
  3. 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.

¿Fue útil?

Solución

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:

  • You have a machine with two printers, A and B.
  • Printer A is the default printer.
  • Application starts.
  • User prints, but selects printer B.
  • User prints again. Program remembers that last time round the user wanted printer B. But this time user wants printer A and so changes to that printer.
  • User then changes default printer to printer B in control panel.
  • User goes to print again. Which printer should be offered to the user?

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?

Otros consejos

Just add : Printer.Refresh;

Like this :

procedure TForm1.Button1Click(Sender: TObject); begin Printer.Refresh; ShowMessage(Printer.Printers[Printer.PrinterIndex]); end;

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top