Delphi 获取打印机,打印队列中任务数量
项目中使用标签打印机打印标签,由于客户是“自由爱好者”,处理两次“无法打印”的技术咨询后,写了个函数:
虽然CSDN语法中都没有了Delphi,但是WEBURL调用打印机快速打印不干胶标签,Delphi是我效率最高的选项。
//打印机队列任务数 失败返回 -1,否则为队列任务数量function GetPrinterTaskCount(PrinterName:String):Integer;implementationuses winspool;function GetPrinterTaskCount(PrinterName:String):Integer;var pHandle:THandle; LPStrBuf: Array [0..256] of Char; // 打印机名称 NoJobs:Word; // 希望获取的作业数 len:LongWord; // Job_Info 的字节数 Job_Info: array[0..10] of JOB_INFO_1; // 打印队列信息 cbNeeded:Cardinal; cReturned:Cardinal; //队列数量 ret:LongBool;begin Result:=-1; if OpenPrinter('GP-5830 Series',pHandle,nil) then try len:= Sizeof(Job_Info); cbNeeded:=0; cReturned:=0; NoJobs:=10 ; if EnumJobs(pHandle,0 ,NoJobs,1,@Job_Info,len,cbNeeded,cReturned) then begin result:= cReturned; end else Result:=-1; finally if ( pHandle 0 ) then ClosePrinter( pHandle ); end;end;
ok,加到项目中:
p:=nil; if cbx_Gprinter.ItemIndex<0 then begin isExit:=True; FTerminated:=False; Show(); ShowMessage('请安装并设置,标签打印机!'); Exit; end else begin i:=GetPrinterTaskCount(cbx_Gprinter.Text); if i>0 then begin isExit:=True; FTerminated:=true; //Show(); ShowMessage('打印机:'+cbx_Gprinter.Text+' 中存在 【 '+IntToStr(i)+' 】个打印任务,请等待打印完成或清空队列后再试!'); Exit; end; end;
不解释了: