捕获打印机状态改变
unit Unit1;
interface
uses
Windows, Messages,
SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
WinSpool,
StdCtrls;
var
pi2:PRINTER_INFO_2;
pno:PRINTER_NOTIFY_OPTIONS;
pinfo:PPrinterNotifyInfo;
//pinty:pPrinterNotifyOptionsType;
hResult:THandle;
pdwChange:DWORD;
Result:LongBool;
bo:boolean;
i:integer;
pn:array[0..1]
of PRINTER_NOTIFY_OPTIONS_TYPE;
pnf:array[0..100] of
WORD;
jnf:array[0..100] of
WORD;
pBuf:Pchar;
pd:PRINTER_NOTIFY_INFO_DATA;
ss:string;
awd:^DWORD;
st : TStringList;
dec : string;
s:array[1..100] of
string;
ts:string;
begin
pdwChange:=0;
bo:=false;
// FillChar(pno, SizeOf(pno), 0);
pno.Version
:=2;
pno.Flags :=PRINTER_NOTIFY_OPTIONS_REFRESH;
pno.Count:=2;
pno.pTypes:=@pn;
pnf[0]:=PRINTER_NOTIFY_FIELD_STATUS;
pnf[1]:=PRINTER_NOTIFY_FIELD_CJOBS;
pnf[2]:=PRINTER_NOTIFY_FIELD_ATTRIBUTES;
pnf[3]:=PRINTER_NOTIFY_FIELD_COMMENT;
pnf[4]:=PRINTER_NOTIFY_FIELD_DEVMODE;
pnf[5]:=PRINTER_NOTIFY_FIELD_LOCATION;
pnf[6]:=PRINTER_NOTIFY_FIELD_SECURITY_DESCRIPTOR;
pnf[7]:=PRINTER_NOTIFY_FIELD_SEPFILE;
pn[0].wType:=PRINTER_NOTIFY_TYPE;
pn[0].Count:=8;
pn[0].pFields:=@pnf;
jnf[0]:=JOB_NOTIFY_FIELD_DOCUMENT;
jnf[1]:=JOB_NOTIFY_FIELD_STATUS;
jnf[2]:=JOB_NOTIFY_FIELD_MACHINE_NAME;
jnf[3]:=JOB_NOTIFY_FIELD_PORT_NAME;
jnf[4]:=JOB_NOTIFY_FIELD_USER_NAME;
jnf[5]:=JOB_NOTIFY_FIELD_NOTIFY_NAME;
jnf[6]:=JOB_NOTIFY_FIELD_DATATYPE;
jnf[7]:=JOB_NOTIFY_FIELD_PRINT_PROCESSOR;
jnf[8]:=JOB_NOTIFY_FIELD_PARAMETERS;
jnf[9]:=JOB_NOTIFY_FIELD_DRIVER_NAME;
jnf[10]:=JOB_NOTIFY_FIELD_DEVMODE;
jnf[11]:=JOB_NOTIFY_FIELD_STATUS_STRING;
jnf[12]:=JOB_NOTIFY_FIELD_SECURITY_DESCRIPTOR;
jnf[13]:=JOB_NOTIFY_FIELD_PRINTER_NAME;
jnf[14]:=JOB_NOTIFY_FIELD_PRIORITY;
jnf[15]:=JOB_NOTIFY_FIELD_POSITION;
jnf[16]:=JOB_NOTIFY_FIELD_SUBMITTED;
jnf[17]:=JOB_NOTIFY_FIELD_START_TIME;
jnf[18]:=JOB_NOTIFY_FIELD_UNTIL_TIME;
jnf[19]:=JOB_NOTIFY_FIELD_TIME;
jnf[20]:=JOB_NOTIFY_FIELD_TOTAL_PAGES;
jnf[21]:=JOB_NOTIFY_FIELD_PAGES_PRINTED;
jnf[22]:=JOB_NOTIFY_FIELD_TOTAL_BYTES;
jnf[23]:=JOB_NOTIFY_FIELD_BYTES_PRINTED;
pn[1].wType:=JOB_NOTIFY_TYPE;
pn[1].Count:=24;
pn[1].pFields:=@jnf;
pi2.pPrinterName := 'HP LaserJet 4
';
if OpenPrinter(pi2.pPrinterName ,hPrinter,0) then
hResult :=
FindFirstPrinterChangeNotification(hPrinter,PRINTER_CHANGE_JOB,0,@pno);
if hResult <> INVALID_HANDLE_VALUE then
bo:=
true;
while bo do
begin
WaitForSingleObject(hResult,INFINITE);
pno.Flags:=0;
Result :=
FindNextPrinterChangeNotification(hResult,pdwChange,@pno,pointer(pinfo));
if ord(Result) <> 0 then
if(pdwChange and
PRINTER_CHANGE_ADD_JOB)> 0 then
// showmessage( '添加打印工作
');
for i:=0 to pinfo.Count do
begin
pbuf:=pinfo.aData[i].NotifyData.Data.pBuf;
awd:=@pinfo.aData[i].NotifyData.adwData;
ss:=string(pbuf);
if(pinfo.aData[i].wType=PRINTER_NOTIFY_TYPE) then
begin
//showmessage( '打印机通知 ');
if(pinfo.aData[i].Field=PRINTER_NOTIFY_FIELD_PRINTER_NAME)
then
//showmessage( '打印机名称 ');
showmessage(ss);
if(pinfo.aData[i].Field=PRINTER_NOTIFY_FIELD_SHARE_NAME)
then
//showmessage( '共享名 ');
showmessage(ss);
end;
if(pinfo.aData[i].wType=JOB_NOTIFY_TYPE) then
begin
//showmessage( '工作信息 ');
//showmessage(ss);
if(pinfo.aData[i].Field=JOB_NOTIFY_FIELD_PRINTER_NAME)
then
showmessage(ss);
if(pinfo.aData[i].Field=JOB_NOTIFY_FIELD_SUBMITTED)
then
showmessage(ss);
if(pinfo.aData[i].Field=JOB_NOTIFY_FIELD_MACHINE_NAME)
then
showmessage(ss);
//showmessage( '机器名 ');
if(pinfo.aData[i].Field=JOB_NOTIFY_FIELD_USER_NAME)
then
//showmessage( '用户名 ');
showmessage(ss);
if(pinfo.aData[i].Field=JOB_NOTIFY_FIELD_DOCUMENT)
then
//showmessage( '文档名 ');
showmessage(ss);
end;
end;
end;
end;