X
xwufan
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码是我写的一个统计打印页数的程序,可是他可以正常显示打印任务、调用者
可是总页数和已经打印的页数却始终是0,谁能告诉我为什么
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Winspool, Printers, ExtCtrls;
type
TMainFrm = class(TForm)
Lbl: TLabel;
API: TButton;
PrinterLst: TListBox;
Label1: TLabel;
Label2: TLabel;
INT: TButton;
GetPrinterJob: TButton;
procedure APIClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainFrm: TMainFrm;
hPrinter:THandle;
implementation
{$R *.dfm}
procedure TMainFrm.APIClick(Sender: TObject);
var
Needed,ReturnedWORD;
i,Count:integer;
tbt:byte;
pNeededDword;
P2JobInfo1;
PPIPRINTERINFO2;
Printer:TPrinter;
Device,Driver,Port:array [0..255] of Char;
PPr,DevMode:Cardinal;
begin
Printer:=TPrinter.Create;
//创建Printer对象
//取得打印机的名称,驱动程序,端口号
Printer.GetPrinter(Device,Driver,Port,DevMode);
//取得打印机的句柄
If NOT OpenPrinter(@device,hPrinter,nil) then
begin
ShowMessage('OpenPrinter'+SysErrorMessage(GetLastError));
Exit;
end;
//分配内存空间
PNeeded:=AllocMem(255);
P2:=AllocMem(1024);
PPI:=AllocMem(1024);
TRy
//取得打印机队列中的任务数
If NOT GetPrinter(hPrinter,2,PPI,2048,PNeeded) then
begin
ShowMessage('GetPrinter'+SysErrorMessage(GetLastError));
exit;
end;
If PPI.cJobs<=0 then
begin
ShowMessage('没有打印任务');
Exit;
end;
//取得打印机状态
If Enumjobs(hPrinter,0,1,2,p2,2048,Needed,Returned) then
begin
If P2.JobId >0 then
begin
//显示打印机状态数据
PrinterLst.Clear;
PrinterLst.Items.Add(P2.pusername);
PrinterLst.Items.Add(INTTOSTR(P2.TotalPages));
PrinterLst.Items.Add(INTTOSTR(PPI.cJobs));
PrinterLst.Items.Add(IntToStr(P2.PagesPrinted));
PrinterLst.Items.Add(P2.pDocument );
end
end
else
ShowMessage('Enumjobs'+SysErrorMessage(GetLastError));
Finally
//关闭打印机,释放内存空间
ClosePrinter(hPrinter);
Printer.Free;
FreeMem(PNeeded);
FreeMem(p2);
FreeMem(PPI);
end;
end;
--
可是总页数和已经打印的页数却始终是0,谁能告诉我为什么
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Winspool, Printers, ExtCtrls;
type
TMainFrm = class(TForm)
Lbl: TLabel;
API: TButton;
PrinterLst: TListBox;
Label1: TLabel;
Label2: TLabel;
INT: TButton;
GetPrinterJob: TButton;
procedure APIClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainFrm: TMainFrm;
hPrinter:THandle;
implementation
{$R *.dfm}
procedure TMainFrm.APIClick(Sender: TObject);
var
Needed,ReturnedWORD;
i,Count:integer;
tbt:byte;
pNeededDword;
P2JobInfo1;
PPIPRINTERINFO2;
Printer:TPrinter;
Device,Driver,Port:array [0..255] of Char;
PPr,DevMode:Cardinal;
begin
Printer:=TPrinter.Create;
//创建Printer对象
//取得打印机的名称,驱动程序,端口号
Printer.GetPrinter(Device,Driver,Port,DevMode);
//取得打印机的句柄
If NOT OpenPrinter(@device,hPrinter,nil) then
begin
ShowMessage('OpenPrinter'+SysErrorMessage(GetLastError));
Exit;
end;
//分配内存空间
PNeeded:=AllocMem(255);
P2:=AllocMem(1024);
PPI:=AllocMem(1024);
TRy
//取得打印机队列中的任务数
If NOT GetPrinter(hPrinter,2,PPI,2048,PNeeded) then
begin
ShowMessage('GetPrinter'+SysErrorMessage(GetLastError));
exit;
end;
If PPI.cJobs<=0 then
begin
ShowMessage('没有打印任务');
Exit;
end;
//取得打印机状态
If Enumjobs(hPrinter,0,1,2,p2,2048,Needed,Returned) then
begin
If P2.JobId >0 then
begin
//显示打印机状态数据
PrinterLst.Clear;
PrinterLst.Items.Add(P2.pusername);
PrinterLst.Items.Add(INTTOSTR(P2.TotalPages));
PrinterLst.Items.Add(INTTOSTR(PPI.cJobs));
PrinterLst.Items.Add(IntToStr(P2.PagesPrinted));
PrinterLst.Items.Add(P2.pDocument );
end
end
else
ShowMessage('Enumjobs'+SysErrorMessage(GetLastError));
Finally
//关闭打印机,释放内存空间
ClosePrinter(hPrinter);
Printer.Free;
FreeMem(PNeeded);
FreeMem(p2);
FreeMem(PPI);
end;
end;
--