Delphi Programming / Object Pascal
サイトのトップページへ リンクのページへ ロゴマーク
[掲載 2004年02月14日] [更新 2016年04月20日] プリンタ設定コンポーネント
   
プリンタ設定コンポーネント
テストプログラム PaperList
動作確認等 Windows 7 U64(SP1) + Delphi XE(UP1) Pro







用紙名の一覧表示と設定


PaperList プロパティと SetPaperName,SetOrientation メソッドのテストプログラムです.リストで選択した用紙サイズを設定します.


図1
設計時画面
図2
実行画面
  • 用紙サイズは A4 と A5 だけリスト

リスト1
テストプログラムのコード
unit PaperListUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, plSetPrinter, Buttons;

type
  TForm1 = class(TForm)
    plSetPrinter1: TplSetPrinter;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//=============================================================================
//  コンボボックスに用紙名のリストを作成
//  用紙名に'A5'と'A4'のもののみリストする
//=============================================================================
procedure TForm1.FormShow(Sender: TObject);
var
  LPaperNo   : Integer;
  LPaperName : string;
begin
  ComboBox1.Clear;
  //コンボボックスは編集不可に
  ComboBox1.Style := csDropDownList;
  for LPaperNo := plSetPrinter1.PaperList.Count - 1 downto 0 do begin
    LPaperName := plSetPrinter1.PaperList[LPaperNo];
    if (Pos('A5', LPaperName) <> 0) or (Pos('A4' , LPaperName) <> 0) then begin
      ComboBox1.Items.Add(LPaperName);
    end;
  end;

  //現在の設定用紙名があればそれを選択
  //なければリスト先頭の用紙を選択状態にする
  LPaperName := plSetPrinter1.PaperName;
  ComboBox1.ItemIndex := 0;
  for LPaperNo := 0 to ComboBox1.Items.Count - 1 do begin
    if ComboBox1.Items[LPaperNo] = LPaperName then begin
      ComboBox1.ItemIndex := LPaperNo;
      Break;
    end;
  end;
end;

//=============================================================================
//  コードで直接用紙サイズを設定する(用紙サイズ番号で)
//=============================================================================
procedure TForm1.Button2Click(Sender: TObject);
var
  LText : string;
begin
  if plSetPrinter1.SetPaperNumber(DMPAPER_A3) = False then begin
    LText := '用紙サイズ A3 には設定できません,';
    MessageBox(Handle, PChar(LText), '設定不可', MB_ICONWARNING);
  end;

  //念のため[プリンタの設定ダイアログ]を表示
  plSetPrinter1.ShowDialog;
end;

//=============================================================================
//  プリンタのプロパティダイアログを表示
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
begin
  //コンボボックスのテキストで用紙名をセット
  plSetPrinter1.SetPaperName(ComboBox1.Items[ComboBox1.ItemIndex]);

  //設定確認のためにプロパティのダイアログを表示
  plSetPrinter1.ShowDialog;
end;

end.