{ 关于打印监控,下面是一个简单的示例,可以监控到打印的任务信息。 } unit FfrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, ComCtrls, StdCtrls, Buttons,WinSpool; type TfrmMain = class(TForm) ListView1: TListView; Panel1: TPanel; BitBtn2: TBitBtn; Timer1: TTimer; procedure Timer1Timer(Sender: TObject); private CurrentMachineName,CurrentUser,CurrentDocName,CurrentPrinter,CurrentPages: String; procedure PrintDetect; procedure SaveData; { Private declarations } public { Public declarations } end; var frmMain: TfrmMain; implementation {$R *.dfm} procedure TfrmMain.PrintDetect; var tbt:Byte; PrinterNeeded,PrinterReturned,JobNeeded,JobReturned:DWORD; PrinterResultBuffer,JobResultBuffer:Pointer; PrinterCount,JobCount:Integer; PrinterInfo:PPrinterInfo1; //打印机队列 JobInfo:PJobInfo2; //打印任务队列 hPrinter:THandle; begin PrinterNeeded:=0; PrinterReturned:=0; EnumPrinters(PRINTER_ENUM_lOCAL,'',1,@tbt,0,PrinterNeeded,PrinterReturned); if PrinterNeeded<>0 then begin PrinterCount:=PrinterNeeded; GetMem(PrinterResultBuffer,PrinterCount); EnumPrinters(PRINTER_ENUM_lOCAL,'',1,PrinterResultBuffer,PrinterNeeded,PrinterNeeded,PrinterReturned); PrinterInfo:=PrinterResultBuffer; //当前打印机 JobNeeded:=0; OpenPrinter(PrinterInfo^.pName,hPrinter,nil); EnumJobs(hPrinter,0,1,2,@tbt,0,JobNeeded,JobReturned); if JobNeeded<>0 then begin JobCount:=JobNeeded; GetMem(JobResultBuffer,JobCount); try //列举当前正在打印的任务 EnumJobs(hPrinter,0,1,2,JobResultBuffer,JobNeeded,JobNeeded,JobReturned); JobInfo:=JobResultBuffer; { AtPrintEdit.Text:='计算机名:'+(JobInfo^.pMachineName)+ ' 用户名:'+String(JobInfo^.pUserName)+ ' 文档名:'+String(JobInfo^.pDocument); } if CurrentDocName<>JobInfo^.pDocument then begin if CurrentDocName<>'' then SaveData; CurrentMachineName:=JobInfo^.pMachineName; CurrentUser:=JobInfo^.pUserName; CurrentDocName:=JobInfo^.pDocument; CurrentPrinter:=JobInfo^.pPrinterName; CurrentPages:=InttoStr(JobInfo^.TotalPages); end; finally FreeMem(JobResultBuffer,JobCount); end; end else begin if CurrentDocName<>'' then begin SaveData; CurrentMachineName:=''; CurrentUser:=''; CurrentDocName:=''; CurrentPrinter:=''; CurrentPages:=''; end; end; ClosePrinter(hPrinter); end; end; procedure TfrmMain.SaveData; var ListItem:TListItem; begin ListItem:=ListView1.Items.Add; ListItem.Caption:=CurrentMachineName; ListItem.SubItems.Add(CurrentUser); ListItem.SubItems.Add(DatetoStr(Date)+' '+TimeTostr(Time)); ListItem.SubItems.Add(CurrentDocName); ListItem.SubItems.Add(CurrentPrinter); ListItem.SubItems.Add(CurrentPages); end; procedure TfrmMain.Timer1Timer(Sender: TObject); begin PrintDetect; end; end.