お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"指定のプリンタでPrinterSetupDialog表示"

この発言に対し以下のコメントが寄せられています
#00617 Satobe さん RE:指定のプリンタでPrinterSetupDialog表

◆解説 TPrinterSetupDialog で、指定のプリンタを初期値として表示させる。 【「通常使うプリンタ」でないプリンタ】(ややこしい(^^;) の PrinterSetupDialogを表示できます。 ◆動作確認 Delphi3.1 + Win95a ◆例 (1) uses に Printers を追加する (2) Form1に ListBox1,Button1,PrinterSetupDialog1を置く ----- ここから ----- type TForm1 = class(TForm) ...省略 private { Private 宣言 } FPrinterName: String; //←追加する ...省略 implementation //Form1 OnCreateイベント procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Items.Assign(Printer.Printers); //通常使うプリンタを選択 Printer.PrinterIndex := -1; ListBox1.ItemIndex := Printer.PrinterIndex; end; //PrinterSetupDialog1 OnShowイベント procedure TForm1.PrinterSetupDialog1Show(Sender: TObject); var PNameComboBox: HWND; index: Integer; begin //名前(N)コンボボックスのウィンドウハンドルを取得 PNameComboBox := GetDlgItem(TPrinterSetupDialog(Sender).Handle, $470); if PNameComboBox <> HWND(nil) then begin //コンボボックスのアイテムからFPrinterNameを検索 index := SendMessage(PNameComboBox, CB_FINDSTRING, -1, Longint(PChar(FPrinterName))); if index >= 0 then //FPrinterNameを見つけた SendMessage(PNameComboBox, CB_SETCURSEL, index, 0); end; end; //Button1 OnClickイベント procedure TForm1.Button1Click(Sender: TObject); var Device, Driver, Port: array[0..79] of char; DeviceMode: THandle; begin if ListBox1.ItemIndex < 0 then begin ShowMessage('ListBoxで目的のプリンタを選択して下さい'); Exit; end; //ListBox1で選択されたプリンタの「プリンタ名」を取得する Printer.PrinterIndex := ListBox1.ItemIndex; Printer.GetPrinter(Device, Driver, Port, DeviceMode); Printer.SetPrinter(Device, Driver, Port, 0); Printer.GetPrinter(Device, Driver, Port, DeviceMode); if DeviceMode = 0 then begin ShowMessage('Printer.GetPrinter エラー'); Exit; end; FPrinterName := Device; PrinterSetupDialog1.Execute; end; ----- ここまで ----- ListBoxで目的のプリンタを選択してから Buttonをクリックして 下さい。 Windowsのプリンタ設定は、DEVMODE構造体と呼ばれるデータによっ て管理されています。 Delphiの TPrinterオブジェクトは、このDEVMODE構造体を内部で 保持しているのですが、PrinterIndexプロパティを書き換えても DEVMODE構造体の再取得を行ってくれないようです。 そこで、PrinterIndexプロパティを書き換えた後、 Printer.GetPrinter(Device, Driver, Port, DeviceMode); Printer.SetPrinter(Device, Driver, Port, 0); この2行を実行することで、指定のプリンタの DEVMODE構造体を 再取得させています。 その後、さらに Printer.GetPrinter(Device, Driver, Port, DeviceMode); if DeviceMode = 0 then begin この処理を行うことで、DEVMODE構造体の再取得に成功したかどう かを判定しています。 #カスタムコンポ化してしまった方が便利かも知れませんね。(^^) Original document by Satobe 氏 ID:(JCG00336)



ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum