盒子上面的東西,大家借鑑借鑑:
unit UTrdGetJobs;
interface
uses
Classes,winspool,Windows,SysUtils;
type
TCallBack=Procedure(JobRecord:String) of Object;
TGetJobs = class(TThread)
private
protected
FPrinterName:String;
FCallBack:TCallBack;
Function GetJob():String;Virtual;Abstract;
procedure Execute; override;
Public
constructor Create(PrinterName:String;ACallBack:TCallBack);
end;
TGetJobs9X=Class(TGetJobs)
Protected
Function GetJob():String;Override;
end;
TGetJobsNT=Class(TGetJobs)
Protected
Function GetJob():String;Override;
end;
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like,
procedure TrdGetJobs.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TrdGetJobs }
constructor TGetJobs.Create(PrinterName: String; ACallBack: TCallBack);
begin
FPrinterName:=PrinterName;
FCallBack:=ACallBack;
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TGetJobs.Execute;
begin
while not Terminated do
begin
FCallBack(GetJob);
end;
end;
{ TGetJobsNT }
function TGetJobsNT.GetJob: String;
Const
InfoLevel = 1;
FirstJob = 0;
LastJob = 19;
Var
Jobs: Array[FirstJob..LastJob] Of TJobInfo1;
BytesNeeded,NumJobs: Dword;
hPrinter:THandle;
JobsRecord:String;
begin
JobsRecord:=';
if OpenPrinter(Pchar(FPrinterName),hPrinter,nil) then
begin
WaitForPrinterChange(hPrinter,PRINTER_CHANGE_ADD_JOB);
If EnumJobs(hPrinter,FirstJob,LastJob+1,InfoLevel,@Jobs,SizeOf(Jobs),BytesNeeded,NumJobs) Then
begin
if NumJobs<>0 then
begin
With Jobs[NumJobs-1] Do
begin
JobsRecord:=JobsRecord+StrPas(pDocument)+#9;
JobsRecord:=JobsRecord+StrPas(pPrinterName)+#9;
JobsRecord:=JobsRecord+StrPas(pMachineName)+#9;
JobsRecord:=JobsRecord+StrPas(pUserName)+#9;
JobsRecord:=JobsRecord+IntToStr(TotalPages)+#9;
JobsRecord:=JobsRecord+DateTimeToStr(SystemTimeToDateTime(Submitted)+8/24)+#9;
end;
end;
end;
ClosePrinter(hPrinter);
end;
Result:=JobsRecord;
end;
{ TGetJobs9X }
function TGetJobs9X.GetJob: String;
begin
//
end;
end.