其中主要程序如下:
type
TMainFrm = class(TForm)
Pages: TPageControl;
PrinterSht: TTabSheet;
PrinterLst: TListBox;
...
private
procedure SpollerStatus(var Msg:TWMSpoolerStatus); message WM_SPOOLERSTATUS;
...
uses
WinSpool, ExtCtrls;
procedure TMainFrm.SpollerStatus(var Msg:TWMSpoolerStatus);
var
tbt:Byte;
Needed,Returned:DWORD;
ResultBuffer:Pointer;
i,x,Count:Integer;
Pi:PJobInfo1;
hPrinter:THandle;
List:TListBox;
Lbl:TLabel;
begin
for i:=1 to Pages.PageCount-1 do begin
hPrinter:=Pages.Pages[i].Tag;
List:=Pages.Pages[i].Controls[0] as TListBox;
Lbl:=((Pages.Pages[i].Controls[1] as TPanel)
.Controls[0] as TLabel);
List.Clear;
EnumJobs(hPrinter,0,Msg.JobsLeft,1,@tbt,0,Needed,Returned);
if Needed=0 then begin
Lbl.Caption:='0 个作业';
Continue;
end;
Count:=Needed;
if Count<=0 then begin
Lbl.Caption:='0 个作业';
Continue;
end;
GetMem(ResultBuffer,Count);
try
EnumJobs(hPrinter,0,Msg.JobsLeft,1,ResultBuffer,Needed,Needed,Returned);
if Returned>Msg.JobsLeft then begin
Lbl.Caption:='0 个作业';
Continue;
end;
Lbl.Caption:=IntToStr(Returned)+' 个作业';
Pi:=ResultBuffer;
for x:=0 to Returned-1 do begin
List.Items.Add(Pi^.pDocument);
Pi:=PJobInfo1(LongInt(Pi)+Sizeof(Pi^));
end;
finally
FreeMem(ResultBuffer,Count);
end;
end;
Msg.Result:=0;
end;
procedure TMainFrm.FormCreate(Sender: TObject);
var
tbt:Byte;
Needed,Returned:DWORD;
ResultBuffer:Pointer;
x,Count:Integer;
Pi:PPrinterInfo1;
Sheet:TTabSheet;
hPrinter:THandle;
Panel:TPanel;
begin
PrinterLst.Clear;
EnumPrinters(PRINTER_ENUM_LOCAL,'',1,@tbt,0,Needed,Returned);
if Needed=0 then Exit;
Count:=Needed;
GetMem(ResultBuffer,Count);
EnumPrinters(PRINTER_ENUM_LOCAL,'',1,ResultBuffer,Needed,Needed,Returned);
Pi:=ResultBuffer;
for x:=0 to Returned-1 do begin
PrinterLst.Items.Add(Pi^.pName);
Pi:=PPrinterInfo1(LongInt(Pi)+Sizeof(Pi^));
Sheet:=TTabSheet.Create(Self);
with Sheet do begin
PageControl:=Pages;
Caption:=PrinterLst.Items[PrinterLst.Items.Count-1];
OpenPrinter(PChar(Caption),hPrinter,nil);
Tag:=hPrinter;
ClosePrinter(hPrinter);
with TListBox.Create(Self) do begin
Parent:=Sheet;
Align:=alClient;
end;
Panel:=TPanel.Create(Self);
with Panel do begin
Parent:=Sheet;
Align:=alTop;
Height:=20;
BevelOuter:=bvNone;
end;
with TLabel.Create(Self) do begin
Parent:=Panel;
Left:=10;
Top:=3;
Caption:='0 个作业';
end;
end;
end;
FreeMem(ResultBuffer,Count);
end;
procedure TMainFrm.FormDestroy(Sender: TObject);
var
i,j:Integer;
begin
for i:=1 to Pages.PageCount-1 do
with Pages.Pages[i] do begin
((Controls[1] as TPanel).Controls[0] as TLabel).Free;
for j:=0 to ControlCount-1 do
Controls[0].Free;
Free;
end;
end;