お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





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

"Timer によるプリントジョブの監視"







■概要
 Timer によるプリントジョブ監視のサンプルです。

■コンポーネント
 Form1 に Timer1( Enabled を False にセット), Edit1, Memo1, Button1,
Button2 を適当に配置して下さい。

■サンプル実行時
 Edit1 に監視するプリンタ名を入力し、Button1 をクリックすることで監視
を開始し、プリントジョブがあれば、Memo1 にマシン名・ユーザー名・ドキュ
メント名を表示します。

■注意事項
 ・Windows98SE + Delphi5 Pro で動作確認。
 ・プリントジョブに干渉してしまうことがあります。(((^^;;
 ・例外処理等が少々いい加減なのでご注意下さい。  (((((^^;;;;
 ・本サンプルは、ご自身の責任においてお試し下さい。m(_"_)m

■その他
 ・nifty:FDELPHI/MES/06/6826 参照。深謝 m(_"_)m > TenPuLa 様。
 ・NT系であれば、 Thread を生成し、FindFirstPrinterChangeNotification
  関係の API と WaitForSingleObject等の Wait function を組み合わせて
  プリントサーバーをモニタするのが常道??

■サンプルコード
//=========================================================================
{...略...}
  private
    { Private 宣言 }
    procedure ClosePrntr;
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
  Winspool;

var
  hPrinter: THandle;
{*************************************************************************
   Form1 OnCreate
//***********************************************************************}
procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Font.Name := 'MS ゴシック';
  Button1.Caption := 'Start!';
  Button2.Caption := 'Stop!!';
  Button2.Enabled := False;
  hPrinter := INVALID_HANDLE_VALUE;
end;
{*************************************************************************
   Form1 OnCloseQuery
//***********************************************************************}
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  try
    ClosePrntr;
  except
    CanClose := MessageDlg('ClosePrinter に失敗しましたが終了しますか?',
                                         mtError, mbOKCancel, 0 ) = mrOk;
  end;
end;
{*************************************************************************
   Timer1 OnTimer
   ・指定されたプリンタに対するジョブを監視する。
//***********************************************************************}
procedure TForm1.Timer1Timer(Sender: TObject);
var
  pJobInf1,
  pJobInf1Curr: PJobInfo1;
  i,
  cbNeeded,
  JobCount,
  pcReturned  : Cardinal;
const
  JobInf1Size = SizeOf( TJobInfo1 );
  {-- Job 数取得 --------------------------------------------------------}
  function GetJobCount: Cardinal;
  var
    pPrntrInf2: PPrinterInfo2;
  begin
    GetPrinter( hPrinter, 2, nil, 0, @cbNeeded );
    pPrntrInf2 := AllocMem( cbNeeded );
    try
      Win32Check(
        GetPrinter( hPrinter, 2, pPrntrInf2, cbNeeded, @cbNeeded )
      );
      Result := pPrntrInf2^.cJobs;
    finally
      FreeMem( pPrntrInf2 );
    end;{try..finally..( pPrntrInf2 := AllocMem( cbNeeded ); )}
  end;
  {-- End (Job 数取得) --------------------------------------------------}
begin
  Timer1.Enabled := False;
  Memo1.Clear;
  JobCount := GetJobCount;
  if JobCount > 0 then
    begin
      EnumJobs( hPrinter, 0, JobCount, 1, nil, 0, cbNeeded, pcReturned );
      pJobInf1 := AllocMem( cbNeeded );
      try
        Win32Check(
          EnumJobs( hPrinter, 0, JobCount, 1,
                    pJobInf1, cbNeeded, cbNeeded, pcReturned )
        );
        pJobInf1Curr := pJobInf1;
        for i := 0 to Pred( pcReturned ) do begin
          with pJobInf1Curr^, Memo1.Lines do begin
            Add( 'Machine : ' + String( pMachineName ));
            Add( 'User    : ' + String( pUserName    ));
            Add( 'Document: ' + String( pDocument    ));
            Add( '' );
          end;{with pJobInf1Curr^, Memo1.Lines do}
          Inc( PChar( pJobInf1Curr ), JobInf1Size );
        end;{for i := 0 to Pred( pcReturned ) do}
      finally
        FreeMem( pJobInf1 );
      end;{try..finally..( pJobInf1 := AllocMem( cbNeeded ); )}
    end;{if JobCount > 0 then}
  Timer1.Enabled := True;
end;
{*************************************************************************
   Button1 OnClick
   ・プリンタオブジェクトをオープンし監視開始。
//***********************************************************************}
procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Enabled := False;
  try
    Win32Check( OpenPrinter( PChar( Edit1.Text ), hPrinter, nil ));
    Timer1.Enabled := True;
  except
    Button1.Enabled := True;
    raise;
  end;{try..except..}
  Button2.Enabled := True;
end;
{*************************************************************************
   Button2 OnClick
   ・プリンタの監視を中止する。
//***********************************************************************}
procedure TForm1.Button2Click(Sender: TObject);
begin
  ClosePrntr;
  Button2.Enabled := False;
  Button1.Enabled := True;
end;
{*************************************************************************
   ClosePrntr
   ・プリンタオブジェクトをクローズする。
//***********************************************************************}
procedure TForm1.ClosePrntr;
begin
  Timer1.Enabled := False;
  if hPrinter = INVALID_HANDLE_VALUE then Exit;
  Win32Check( ClosePrinter( hPrinter ));
  hPrinter := INVALID_HANDLE_VALUE;
end;
{*************************************************************************
//***********************************************************************}
{...略...}
//=========================================================================

                              00/12/14(Thu) 03:22pm  Fermion [KHF03264]

 


- FDELPHI  MES(16):玉石混淆みんなで作るSample蔵【見本蓄積】 00/12/22 -

Original document by Fermion         氏 ID:(KHF03264)


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

Copyright 1996-2002 Delphi Users' Forum