私は 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)
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.
窓の下で | 写真 窓の下で コントロール パネル | デバイスとプリンターには、3 つのプリンターがあります。
- CutePDF ライター (デフォルトのプリンター)
- マイファックス
- Microsoft XPS ドキュメント ライター
サンプルアプリケーションを実行して Button1 をクリックすると、デフォルトのプリンターとして「CutePDF Writer」が表示されます。サンプル アプリケーションを閉じずに、Windows | に移動します。コントロール パネル | デバイスとプリンターで「My Fax」をデフォルト プリンターとして設定し、サンプル アプリケーションに戻ってもう一度 Button1 をクリックすると、「CutePDF Writer」がデフォルト プリンターとして表示されます (「My Fax」と表示されるはずです)。ユニット Vcl.Printers でクラス TPrinter を学習した後、次のようにコードを記述できます。
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Printer.Printing then
Printer.PrinterIndex := -1;
ShowMessage(Printer.Printers[Printer.PrinterIndex]);
end;
毎回 PrinterIndex を -1 に設定する必要がある場合、これは適切な方法ではありません。私の質問は、デフォルトのプリンターの変更通知があるかどうかをアプリケーションがどのように知るかです。デフォルトのプリンターの変更通知がある場合にのみ、PrinterIndex を -1 に設定するようにします。