お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





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

"プリンターの給紙方法と用紙サイズを列挙"




インストールされているプリンターの給紙方法と用紙サイズの番号を列挙して確
認する事が出来るプログラムです。
TPrinter での印刷や LightReport2 使用時に給紙方法指定や用紙サイズ指定を
行うための番号を取得することを目的にしています。
Win9x 系列と WinNT 系列で値が違う場合が多々ありますので、対応する必要が
有れば必ず2種類で確認した上で使用します。

FDELPHI のご発言を参考にさせていただいて作成し自分用に使用しているアプリ
です。ソフトとしてアップするほどのものではないと思うのでサンプルとしてあ
げておきます。(LightReport2 のおまけとしてつけてもいいかな^^;)

1. 新規アプリケーションを作成して、フォーム上に ListBox を1つ,
   Memo を2つ, Botton を1つ貼り付けます。

2. uses に Printers, WinSpool を追加

3. フォームのOnCreateイベントに以下をコピー
procedure TForm1.FormCreate(Sender: TObject);
begin
  //使用可能なプリンタの一覧を ListBox1に入れる
  ListBox1.Items.Assign(Printer.Printers);
end;

4. Botton の OnClick イベントに以下をコピー
procedure TForm1.Button1Click(Sender: TObject);
type
  TBinNoArray = array[0..9999] of WORD;
  PBinNoArray = ^TBinNoArray;
  TBinName = array[0..23] of Char;
  TBinNameArray = array[0..9999] of TBinName;
  PBinNameArray = ^TBinNameArray;
  TPaperNoArray = array[0..9999] of WORD;
  PPaperNoArray = ^TPaperNoArray;
  TPaperName = array[0..63] of Char;
  TPaperNameArray = array[0..9999] of TPaperName;
  PPaperNameArray = ^TPaperNameArray;
var
  Device, Driver, Port: array[0..79] of char;
  DeviceMode: THandle;
  DevMode: PDeviceMode;
  BinCount: DWORD;
  pBinNo: PBinNoArray;
  pBinName: PBinNameArray;
  j: Integer;
  PaperCount: DWORD;
  pPaperNo: PPaperNoArray;
  pPaperName: PPaperNameArray;
begin
  beep;
  case Win32Platform of
     2:  Showmessage(Format('Win32Platform=%d : WindowsNT',
           [Win32Platform]));
     else  Showmessage(Format('Win32Platform=%d : Windows95',
             [Win32Platform]));
  end;
  //選択されたプリンタの「給紙方法」を列挙する
  if ListBox1.ItemIndex < 0 then begin
    ShowMessage('ListBoxから目的のプリンタドライバを選択して下さい');
    Exit;
  end;
  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;
  //********************************************************
  DevMode := GlobalLock(DeviceMode);
  try
    BinCount := DeviceCapabilities(Device, Port,
                                   DC_BINS, nil, DevMode);
    if BinCount > 0 then begin
      GetMem(pBinNo,   BinCount * SizeOf(WORD));
      GetMem(pBinName, BinCount * SizeOf(TBinName));
      try
        DeviceCapabilities(Device, Port, DC_BINS,
                           PChar(pBinNo), Devmode);
        DeviceCapabilities(Device, Port, DC_BINNAMES,
                           PChar(pBinName), Devmode);
        Memo1.Clear;
        for j:=0 to BinCount-1 do begin
          Memo1.Lines.Add(
            Format('%5d : %s',[pBinNo^[j], pBinName^[j]])
            );
        end;
      finally
        FreeMem(pBinNo);
        FreeMem(pBinName);
      end;
    end;
    PaperCount := DeviceCapabilities(Device, Port,
                                   DC_PAPERS, nil, DevMode);
    //選択されたプリンタの「用紙サイズ」を列挙する
    if PaperCount > 0 then begin
      GetMem(pPaperNo,   PaperCount * SizeOf(WORD));
      GetMem(pPaperName, PaperCount * SizeOf(TPaperName));
      try
        DeviceCapabilities(Device, Port, DC_PAPERS,
                           PChar(pPaperNo), Devmode);
        DeviceCapabilities(Device, Port, DC_PAPERNAMES,
                           PChar(pPaperName), Devmode);
        Memo2.Clear;
        for j:=0 to PaperCount-1 do begin
          Memo2.Lines.Add(
            Format('%5d : %s',[pPaperNo^[j], pPaperName^[j]])
            );
        end;
      finally
        FreeMem(pPaperNo);
        FreeMem(pPaperName);
      end;
    end;
  finally
    GlobalUnlock(DeviceMode);
  end;
end;

                             2002/03/06(水) 01:24pm  Ohtaka(QZF12401)
 


Original document by Ohtaka          氏 ID:(QZF12401)


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

Copyright 1996-2002 Delphi Users' Forum