使用 Winspool、打印机;
获取当前打印机句柄
检索当前打印机的句柄 @返回当前打印机的 API 打印机句柄 @Desc 使用 WinSpool.OpenPrinter 获取打印机句柄。调用者拥有句柄的所有权,并且一旦不再需要句柄,就必须对其调用 ClosePrinter。不这样做会造成严重的资源泄漏!
在 Uses 子句中需要 Printers 和 WinSpool。@Raises EWin32Error 如果 OpenPrinter 调用失败。
Function GetCurrentPrinterHandle: THandle;
Const
Defaults: TPrinterDefaults = (
pDatatype : nil;
pDevMode : nil;
DesiredAccess : PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER
);
Var
Device, Driver, Port : array[0..255] of char;
hDeviceMode: THandle;
Begin { GetCurrentPrinterHandle }
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
If not OpenPrinter(@Device, Result, @Defaults) Then
RaiseLastWin32Error;
End; { GetCurrentPrinterHandle }
{: Kill all pending jobs on the current printer }
Procedure PurgeJobsOnCurrentPrinter;
Var
hPrinter: THandle;
Begin
hPrinter:= GetCurrentPrinterHandle;
try
If not WinSpool.SetPrinter( hPrinter, 0, nil,
PRINTER_CONTROL_PURGE )
Then
RaiseLastWin32Error;
finally
ClosePrinter( hPrinter );
end;
End; { PurgeJobsOnCurrentPrinter }