delphi 獲取印表機狀態,如缺紙等問題

黑貝是條狗發表於2018-11-15

uses

Printers,WinSpool;

 

 

type
  TPrinterInfo = record
    SeverName: PChar;
    PrinterName: PChar;
    ShareName: PChar;
    PortName: PChar;
    DriverName: PChar;
    Comment: PChar;
    Location: PChar;
    DeviceMode: PDeviceModeW;
    SepFile: PChar;
    PrintProcessor: PChar;
    DataType: PChar;
    Parameters: PChar;
    SecurityDescriptor: PSecurityDescriptor;
    Attributes: Cardinal;
    DefaultPriority: Cardinal;
    StartTime: Cardinal;
    UntilTime: Cardinal;
    Status: Cardinal;
    Jobs: Cardinal;
    AveragePPM: Cardinal;
  end;


function GetCurrentPrinterHandle: THandle;
var
  Device, Driver, Port: array[0..255] of char;
  hDeviceMode: THandle;
begin
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  if not OpenPrinter(@Device, Result, nil) then
    RaiseLastWin32Error;
end;

function GetCurrentPrinterInformation: TPrinterInfo;
var
  hPrinter: THandle;
  pInfo: PPrinterInfo2;
  bytesNeeded: DWORD;
begin
  hPrinter := GetCurrentPrinterHandle;
  try
    Winspool.GetPrinter(hPrinter, 2, Nil, 0, @bytesNeeded);
    pInfo := AllocMem(bytesNeeded);
    try
      Winspool.GetPrinter(hPrinter, 2, pInfo, bytesNeeded, @bytesNeeded);
      Result.SeverName := pInfo^.pServerName;
      Result.PrinterName := pInfo^.pPrinterName;
      Result.ShareName := pInfo^.pShareName;
      Result.PortName := pInfo^.pPortName;
      Result.DriverName := pInfo^.pDriverName;
      Result.Comment := pInfo^.pComment;
      Result.Location := pInfo^.pLocation;
      Result.DeviceMode := pInfo^.pDevMode;
      Result.SepFile := pInfo^.pSepFile;
      Result.PrintProcessor := pInfo^.pPrintProcessor;
      Result.DataType := pInfo^.pDatatype;
      Result.Parameters := pInfo^.pParameters;
      Result.SecurityDescriptor := pInfo^.pSecurityDescriptor;
      Result.Attributes := pInfo^.Attributes;
      Result.DefaultPriority := pInfo^.DefaultPriority;
      Result.StartTime := pInfo^.StartTime;
      Result.UntilTime := pInfo^.UntilTime;
      Result.Status := pInfo^.Status;
      Result.Jobs := pInfo^.cJobs;
      Result.AveragePPM := pInfo^.AveragePPM;
    finally
      FreeMem(pInfo);
    end;
  finally
    ClosePrinter(hPrinter);
  end;
end;



function GetDefaultPrinter(DefaultPrinter: PChar; var I: Integer): BOOL; stdcall;
  external winspl name 'GetDefaultPrinterW';

procedure TForm3.cbb1Change(Sender: TObject);
var
  PrinterInfo: TPrinterInfo;
begin
  PrinterInfo := GetCurrentPrinterInformation;
  memo1.Clear;
  with memo1.Lines do
  begin
    Add('GENERAL   INFORMATION');
    Add('');
    Add('ServerName:   ' + PrinterInfo.SeverName);
    Add('PrinterName:   ' + PrinterInfo.PrinterName);
    Add('ShareName:   ' + PrinterInfo.ShareName);
    Add('PortName:   ' + PrinterInfo.PortName);
    Add('DriverName:   ' + PrinterInfo.DriverName);
    Add('Comment:   ' + PrinterInfo.Comment);
    Add('Location:   ' + PrinterInfo.Location);
    Add('SepFile:   ' + PrinterInfo.SepFile);
    Add('PrintProcessor:   ' + PrinterInfo.PrintProcessor);
    Add('DataType:   ' + PrinterInfo.DataType);
    Add('Parameters:   ' + PrinterInfo.Parameters);
    Add('Attributes:   ' + IntToStr(PrinterInfo.Attributes));
    Add('DefaultPriority:   ' + IntToStr(PrinterInfo.DefaultPriority));
    Add('StartTime:   ' + IntToStr(PrinterInfo.StartTime));
    Add('UntilTime:   ' + IntToStr(PrinterInfo.UntilTime));
    Add('Status:   ' + '0x' + IntToHex(PrinterInfo.Status,8));
    Add('Jobs:   ' + IntToStr(PrinterInfo.Jobs));
    Add('AveragePPM:   ' + IntToStr(PrinterInfo.AveragePPM));
    Add('');
    Add('DEVICEMODE   INFORMATION');
    Add('');

    Add('DeviceName:   ' + PrinterInfo.DeviceMode.dmDeviceName);
    Add('SpecVersion:   ' + IntToStr(PrinterInfo.DeviceMode.dmSpecVersion));
    Add('DriverVersion:   ' + IntToStr(PrinterInfo.DeviceMode.dmDriverVersion));
    Add('Size:   ' + IntToStr(PrinterInfo.DeviceMode.dmSize));
    Add('DriverExtra:   ' + IntToStr(PrinterInfo.DeviceMode.dmDriverExtra));
    Add('Fields:   ' + IntToStr(PrinterInfo.DeviceMode.dmFields));
    Add('Orientation:   ' + IntToStr(PrinterInfo.DeviceMode.dmOrientation));
    Add('PaperSize:   ' + IntToStr(PrinterInfo.DeviceMode.dmPaperSize));
    Add('PaperLength:   ' + IntToStr(PrinterInfo.DeviceMode.dmPaperLength));
    Add('PaperWidth:   ' + IntToStr(PrinterInfo.DeviceMode.dmPaperWidth));
    Add('Scale:   ' + IntToStr(PrinterInfo.DeviceMode.dmScale));
    Add('Copies:   ' + IntToStr(PrinterInfo.DeviceMode.dmCopies));
    Add('DefaultSource:   ' + IntToStr(PrinterInfo.DeviceMode.dmDefaultSource));
    Add('PrintQuality:   ' + IntToStr(PrinterInfo.DeviceMode.dmPrintQuality));
    Add('Color:   ' + IntToStr(PrinterInfo.DeviceMode.dmColor));
    Add('Duplex:   ' + IntToStr(PrinterInfo.DeviceMode.dmDuplex));
    Add('YResolution:   ' + IntToStr(PrinterInfo.DeviceMode.dmYResolution));
    Add('TTOption:   ' + IntToStr(PrinterInfo.DeviceMode.dmTTOption));
    Add('Collate:   ' + IntToStr(PrinterInfo.DeviceMode.dmCollate));
    Add('LogPixels:   ' + IntToStr(PrinterInfo.DeviceMode.dmLogPixels));
    Add('BitsPerPel:   ' + IntToStr(PrinterInfo.DeviceMode.dmBitsPerPel));
    Add('PelsWidth:   ' + IntToStr(PrinterInfo.DeviceMode.dmPelsWidth));
    Add('PelsHeight:   ' + IntToStr(PrinterInfo.DeviceMode.dmPelsHeight));
    Add('DisplayFlags:   ' + IntToStr(PrinterInfo.DeviceMode.dmDisplayFlags));
    Add('DisplayFrequency:   ' + IntToStr(PrinterInfo.DeviceMode.dmDisplayFrequency));
    Add('ICMMethod:   ' + IntToStr(PrinterInfo.DeviceMode.dmICMMethod));
    Add('ICMintent:   ' + IntToStr(PrinterInfo.DeviceMode.dmICMIntent));
    Add('MediaType:   ' + IntToStr(PrinterInfo.DeviceMode.dmMediaType));
    Add('DitherType:   ' + IntToStr(PrinterInfo.DeviceMode.dmDitherType));
    Add('ICCManufacturer:   ' + IntToStr(PrinterInfo.DeviceMode.dmICCManufacturer));
    Add('ICCModel:   ' + IntToStr(PrinterInfo.DeviceMode.dmICCModel));
    Add('PanningWidth:   ' + IntToStr(PrinterInfo.DeviceMode.dmPanningWidth));
    Add('PanningHeight:   ' + IntToStr(PrinterInfo.DeviceMode.dmPanningHeight));
  end;

end;

 procedure TForm3.FormShow(Sender: TObject);
begin
  cbb1.Items.Assign(Printer.Printers);
  cbb1.ItemIndex := 0;
  cbb1.OnChange(nil);
end

status  狀態碼含義:

Printer status value/code

Description

PRINTER_STATUS_BUSY

0x00000200

The printer is busy.

PRINTER_STATUS_DOOR_OPEN

0x00400000

The printer door is open.

PRINTER_STATUS_ERROR

0x00000002

The printer is in an error state.

PRINTER_STATUS_INITIALIZING

0x00008000

The printer is initializing.

PRINTER_STATUS_IO_ACTIVE

0x00000100

The printer is in an active input or output state.

PRINTER_STATUS_MANUAL_FEED

0x00000020

The printer is in a manual feed state.

PRINTER_STATUS_NOT_AVAILABLE

0x00001000

The printer is not available for printing.

PRINTER_STATUS_NO_TONER

0x00040000

The printer is out of toner.

PRINTER_STATUS_OFFLINE

0x00000080

The printer is offline.

PRINTER_STATUS_OUTPUT_BIN_FULL

0x00000800

The printer's output bin is full.

PRINTER_STATUS_OUT_OF_MEMORY

0x00200000

The printer has run out of memory.

PRINTER_STATUS_PAGE_PUNT

0x00080000

The printer cannot print the current page.

PRINTER_STATUS_PAPER_JAM

0x00000008

Paper is stuck in the printer.

PRINTER_STATUS_PAPER_OUT

0x00000010

The printer is out of paper.

PRINTER_STATUS_PAPER_PROBLEM

0x00000040

The printer has an unspecified paper problem.

PRINTER_STATUS_PAUSED

0x00000001

The printer is paused.

PRINTER_STATUS_PENDING_DELETION

0x00000004

The printer is being deleted as a result of a client's call to RpcDeletePrinter. No new jobs can be submitted on existing printer objects for that printer.

PRINTER_STATUS_POWER_SAVE

0x01000000

The printer is in power-save mode.<182>

PRINTER_STATUS_PRINTING

0x00000400

The printer is printing.

PRINTER_STATUS_PROCESSING

0x00004000

The printer is processing a print job.

PRINTER_STATUS_SERVER_OFFLINE

0x02000000

The printer is offline.<183>

PRINTER_STATUS_SERVER_UNKNOWN

0x00800000

The printer status is unknown.<184>

PRINTER_STATUS_TONER_LOW

0x00020000

The printer is low on toner.

PRINTER_STATUS_USER_INTERVENTION

0x00100000

The printer has an error that requires the user to do something.

PRINTER_STATUS_WAITING

0x00002000

The printer is waiting.

PRINTER_STATUS_WARMING_UP

0x00010000

The printer is warming up.

 

相關文章