Nyomtató információk lekérése (PRINTER_INFO_2)

Nyomtató információk lekérése (PRINTER_INFO_2)
2004-10-06T15:28:27+02:00
2004-10-19T09:56:08+02:00
2022-10-22T11:20:38+02:00
guzsolt
Üdv, egy olyan kérdésem lenne hogy hogyan tudnék Delphi alatt egy nyomtatóról a legtöbb információt lekérni, akár a PRINTER_INFO_2 - struktúrán keresztül? Elsősorban a státusza kellene hogy pl lehet-e nyomtatást kiküldeni egy hálózati nyomtatóra, stb...
Jó lenne ha kész forráskóddal is tudnátok segíteni, köszi.

Guzsolt
Mutasd a teljes hozzászólást!
Nos egy hirtelen összelapátolt példa:


unit PrnDiag; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; Label1: TLabel; PrnCaps: TMemo; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure ListBox1Click(Sender: TObject); private procedure GetPrinterProps; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} uses Printers, WinSpool; type TPrinterDevice = class Driver, Device, Port: String; end; const NIN =' (not initialized)'; procedure TForm1.GetPrinterProps; var DeviceMode :THandle; DevMode :PDeviceMode; StubDevMode :TDeviceMode; s :string; hPrinter :THandle; PrnStatus :TPrinterInfo6; PrnInfo2 :PChar; cbNeeded :DWORD; begin PrnCaps.Lines.Clear; s:=TPrinterDevice(Printer.Printers.Objects[ListBox1.ItemIndex]).Device; if not OpenPrinter(PChar(s),hPrinter,NIL) then begin PrnCaps.Lines.Add(SysErrorMessage(GetLastError)); Exit; end; cbNeeded:=0; GetPrinter(hPrinter,2,NIL,0,@cbNeeded); GetMem(PrnInfo2,cbNeeded); try if not GetPrinter(hPrinter,2,PrnInfo2,cbNeeded,@cbNeeded) then PrnCaps.Lines.Add(SysErrorMessage(GetLastError)) else begin PrnCaps.Lines.Add(Format('PrinterName=%s',[PPrinterInfo2(PrnInfo2)^.pPrinterName])); end; finally FreeMem(PrnInfo2); end; // NT/2000/XP !! cbNeeded:=0; if not GetPrinter(hPrinter,6,@PrnStatus,SizeOf(TPrinterInfo6),@cbNeeded) then PrnCaps.Lines.Add(SysErrorMessage(GetLastError)) else begin PrnCaps.Lines.Add(Format('Status=0x%x',[PrnStatus.dwStatus])); if (PrnStatus.dwStatus and PRINTER_STATUS_BUSY)<>0 then PrnCaps.Lines.Add('BUSY') else PrnCaps.Lines.Add('NOT BUSY'); end; DeviceMode:=GlobalAlloc(GHND, DocumentProperties(0, hPrinter, PChar(s), StubDevMode, StubDevMode, 0)); if DeviceMode=0 then begin PrnCaps.Lines.Add(Format('Fail to alloc DevMode structure = %d',[GetLastError])); ClosePrinter(hPrinter); Exit; end; DevMode:=GlobalLock(DeviceMode); try if DocumentProperties(0, hPrinter, PChar(s), DevMode^, DevMode^, DM_OUT_BUFFER)<0 then begin PrnCaps.Lines.Add(Format('Fail to call "DocumentProperties" = %d',[GetLastError])); Exit; end; with DevMode^ do begin PrnCaps.Lines.Add(Format('"Friendly" name of printer (dmDeviceName)=%s',[dmDeviceName])); PrnCaps.Lines.Add(Format('Version of init data (dmSpecVersion)=%d, (0x%x)',[dmSpecVersion,dmSpecVersion])); PrnCaps.Lines.Add(Format('Version of driver by developer (dmDriverVersion)=%d, (0x%x)',[dmDriverVersion,dmDriverVersion])); PrnCaps.Lines.Add(Format('Size of public data (dmSize)=%d',[dmSize])); PrnCaps.Lines.Add(Format('Size of driver-specific data (dmDriverExtra)=%d',[dmDriverExtra])); PrnCaps.Lines.Add(Format('Initialized members (dmFields)=0x%x',[dmFields])); if (dmFields and DM_ORIENTATION)<>0 then PrnCaps.Lines.Add(' DM_ORIENTATION'); if (dmFields and DM_PAPERSIZE)<>0 then PrnCaps.Lines.Add(' DM_PAPERSIZE'); if (dmFields and DM_PAPERLENGTH)<>0 then PrnCaps.Lines.Add(' DM_PAPERLENGTH'); if (dmFields and DM_PAPERWIDTH)<>0 then PrnCaps.Lines.Add(' DM_PAPERWIDTH'); if (dmFields and DM_SCALE)<>0 then PrnCaps.Lines.Add(' DM_SCALE'); if (dmFields and DM_COPIES)<>0 then PrnCaps.Lines.Add(' DM_COPIES'); if (dmFields and DM_DEFAULTSOURCE)<>0 then PrnCaps.Lines.Add(' DM_DEFAULTSOURCE'); if (dmFields and DM_PRINTQUALITY)<>0 then PrnCaps.Lines.Add(' DM_PRINTQUALITY'); if (dmFields and DM_COLOR)<>0 then PrnCaps.Lines.Add(' DM_COLOR'); if (dmFields and DM_DUPLEX)<>0 then PrnCaps.Lines.Add(' DM_DUPLEX'); if (dmFields and DM_YRESOLUTION)<>0 then PrnCaps.Lines.Add(' DM_YRESOLUTION'); if (dmFields and DM_TTOPTION)<>0 then PrnCaps.Lines.Add(' DM_TTOPTION'); if (dmFields and DM_COLLATE)<>0 then PrnCaps.Lines.Add(' DM_COLLATE'); if (dmFields and DM_FORMNAME)<>0 then PrnCaps.Lines.Add(' DM_FORMNAME'); if (dmFields and DM_LOGPIXELS)<>0 then PrnCaps.Lines.Add(' DM_LOGPIXELS'); if (dmFields and DM_BITSPERPEL)<>0 then PrnCaps.Lines.Add(' DM_BITSPERPEL'); if (dmFields and DM_PELSWIDTH)<>0 then PrnCaps.Lines.Add(' DM_PELSWIDTH'); if (dmFields and DM_PELSHEIGHT)<>0 then PrnCaps.Lines.Add(' DM_PELSHEIGHT'); if (dmFields and DM_DISPLAYFLAGS)<>0 then PrnCaps.Lines.Add(' DM_DISPLAYFLAGS'); if (dmFields and DM_DISPLAYFREQUENCY)<>0 then PrnCaps.Lines.Add(' DM_DISPLAYFREQUENCY'); if (dmFields and DM_ICMMETHOD)<>0 then PrnCaps.Lines.Add(' DM_ICMMETHOD'); if (dmFields and DM_ICMINTENT)<>0 then PrnCaps.Lines.Add(' DM_ICMINTENT'); if (dmFields and DM_MEDIATYPE)<>0 then PrnCaps.Lines.Add(' DM_MEDIATYPE'); if (dmFields and DM_DITHERTYPE)<>0 then PrnCaps.Lines.Add(' DM_DITHERTYPE'); s:=Format('Orientation (dmOrientation)=%d',[dmOrientation]); if (dmFields and DM_ORIENTATION)=0 then s:=s+NIN else case dmOrientation of DMORIENT_PORTRAIT :s:=s+' (PORTRAIT)'; DMORIENT_LANDSCAPE:s:=s+' (LANDSCAPE)'; else s:=s+' (UNKNOWN)'; end; PrnCaps.Lines.Add(s); s:=Format('Paper size (dmPaperSize)=%d',[dmPaperSize]); if (dmFields and DM_PAPERSIZE)=0 then s:=s+NIN else case dmPaperSize of DMPAPER_LETTER :s:=s+' (Letter, 8 1/2- by 11-inches)'; DMPAPER_LEGAL :s:=s+' (Legal, 8 1/2- by 14-inches)'; DMPAPER_A4 :s:=s+' (A4 Sheet, 210- by 297-millimeters)'; DMPAPER_CSHEET :s:=s+' (C Sheet, 17- by 22-inches)'; DMPAPER_DSHEET :s:=s+' (D Sheet, 22- by 34-inches)'; DMPAPER_ESHEET :s:=s+' (E Sheet, 34- by 44-inches)'; DMPAPER_LETTERSMALL :s:=s+' (Letter Small, 8 1/2- by 11-inches)'; DMPAPER_TABLOID :s:=s+' (Tabloid, 11- by 17-inches)'; DMPAPER_LEDGER :s:=s+' (Ledger, 17- by 11-inches)'; DMPAPER_STATEMENT :s:=s+' (Statement, 5 1/2- by 8 1/2-inches)'; DMPAPER_EXECUTIVE :s:=s+' (Executive, 7 1/4- by 10 1/2-inches)'; DMPAPER_A3 :s:=s+' (A3 sheet, 297- by 420-millimeters)'; DMPAPER_A4SMALL :s:=s+' (A4 small sheet, 210- by 297-millimeters)'; DMPAPER_A5 :s:=s+' (A5 sheet, 148- by 210-millimeters)'; DMPAPER_B4 :s:=s+' (B4 sheet, 250- by 354-millimeters)'; DMPAPER_B5 :s:=s+' (B5 sheet, 182- by 257-millimeter paper)'; DMPAPER_FOLIO :s:=s+' (Folio, 8 1/2- by 13-inch paper)'; DMPAPER_QUARTO :s:=s+' (Quarto, 215- by 275-millimeter paper)'; DMPAPER_10X14 :s:=s+' (10- by 14-inch sheet)'; DMPAPER_11X17 :s:=s+' (11- by 17-inch sheet)'; DMPAPER_NOTE :s:=s+' (Note, 8 1/2- by 11-inches)'; DMPAPER_ENV_9 :s:=s+' (#9 Envelope, 3 7/8- by 8 7/8-inches)'; DMPAPER_ENV_10 :s:=s+' (#10 Envelope, 4 1/8- by 9 1/2-inches)'; DMPAPER_ENV_11 :s:=s+' (#11 Envelope, 4 1/2- by 10 3/8-inches)'; DMPAPER_ENV_12 :s:=s+' (#12 Envelope, 4 3/4- by 11-inches)'; DMPAPER_ENV_14 :s:=s+' (#14 Envelope, 5- by 11 1/2-inches)';
Mutasd a teljes hozzászólást!

  • ezzel az alapértelmezett nyomtató nevét tudod lekérni:

    uses Printers;

    function GetDefaultPrinterName : string;
    begin
    if (Printer.PrinterIndex > 0)then begin
    Result :=
    Printer.Printers
    [Printer.PrinterIndex];
    end else begin
    Result := '';
    end;
    end;
    Mutasd a teljes hozzászólást!
  • ez a forrás a megoldás a státusz és egyéb info problémádra
    <a href="http://djmada.uw.hu/printer.txt">ez</a>
    Mutasd a teljes hozzászólást!
  • Nos egy hirtelen összelapátolt példa:


    unit PrnDiag; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; Label1: TLabel; PrnCaps: TMemo; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure ListBox1Click(Sender: TObject); private procedure GetPrinterProps; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} uses Printers, WinSpool; type TPrinterDevice = class Driver, Device, Port: String; end; const NIN =' (not initialized)'; procedure TForm1.GetPrinterProps; var DeviceMode :THandle; DevMode :PDeviceMode; StubDevMode :TDeviceMode; s :string; hPrinter :THandle; PrnStatus :TPrinterInfo6; PrnInfo2 :PChar; cbNeeded :DWORD; begin PrnCaps.Lines.Clear; s:=TPrinterDevice(Printer.Printers.Objects[ListBox1.ItemIndex]).Device; if not OpenPrinter(PChar(s),hPrinter,NIL) then begin PrnCaps.Lines.Add(SysErrorMessage(GetLastError)); Exit; end; cbNeeded:=0; GetPrinter(hPrinter,2,NIL,0,@cbNeeded); GetMem(PrnInfo2,cbNeeded); try if not GetPrinter(hPrinter,2,PrnInfo2,cbNeeded,@cbNeeded) then PrnCaps.Lines.Add(SysErrorMessage(GetLastError)) else begin PrnCaps.Lines.Add(Format('PrinterName=%s',[PPrinterInfo2(PrnInfo2)^.pPrinterName])); end; finally FreeMem(PrnInfo2); end; // NT/2000/XP !! cbNeeded:=0; if not GetPrinter(hPrinter,6,@PrnStatus,SizeOf(TPrinterInfo6),@cbNeeded) then PrnCaps.Lines.Add(SysErrorMessage(GetLastError)) else begin PrnCaps.Lines.Add(Format('Status=0x%x',[PrnStatus.dwStatus])); if (PrnStatus.dwStatus and PRINTER_STATUS_BUSY)<>0 then PrnCaps.Lines.Add('BUSY') else PrnCaps.Lines.Add('NOT BUSY'); end; DeviceMode:=GlobalAlloc(GHND, DocumentProperties(0, hPrinter, PChar(s), StubDevMode, StubDevMode, 0)); if DeviceMode=0 then begin PrnCaps.Lines.Add(Format('Fail to alloc DevMode structure = %d',[GetLastError])); ClosePrinter(hPrinter); Exit; end; DevMode:=GlobalLock(DeviceMode); try if DocumentProperties(0, hPrinter, PChar(s), DevMode^, DevMode^, DM_OUT_BUFFER)<0 then begin PrnCaps.Lines.Add(Format('Fail to call "DocumentProperties" = %d',[GetLastError])); Exit; end; with DevMode^ do begin PrnCaps.Lines.Add(Format('"Friendly" name of printer (dmDeviceName)=%s',[dmDeviceName])); PrnCaps.Lines.Add(Format('Version of init data (dmSpecVersion)=%d, (0x%x)',[dmSpecVersion,dmSpecVersion])); PrnCaps.Lines.Add(Format('Version of driver by developer (dmDriverVersion)=%d, (0x%x)',[dmDriverVersion,dmDriverVersion])); PrnCaps.Lines.Add(Format('Size of public data (dmSize)=%d',[dmSize])); PrnCaps.Lines.Add(Format('Size of driver-specific data (dmDriverExtra)=%d',[dmDriverExtra])); PrnCaps.Lines.Add(Format('Initialized members (dmFields)=0x%x',[dmFields])); if (dmFields and DM_ORIENTATION)<>0 then PrnCaps.Lines.Add(' DM_ORIENTATION'); if (dmFields and DM_PAPERSIZE)<>0 then PrnCaps.Lines.Add(' DM_PAPERSIZE'); if (dmFields and DM_PAPERLENGTH)<>0 then PrnCaps.Lines.Add(' DM_PAPERLENGTH'); if (dmFields and DM_PAPERWIDTH)<>0 then PrnCaps.Lines.Add(' DM_PAPERWIDTH'); if (dmFields and DM_SCALE)<>0 then PrnCaps.Lines.Add(' DM_SCALE'); if (dmFields and DM_COPIES)<>0 then PrnCaps.Lines.Add(' DM_COPIES'); if (dmFields and DM_DEFAULTSOURCE)<>0 then PrnCaps.Lines.Add(' DM_DEFAULTSOURCE'); if (dmFields and DM_PRINTQUALITY)<>0 then PrnCaps.Lines.Add(' DM_PRINTQUALITY'); if (dmFields and DM_COLOR)<>0 then PrnCaps.Lines.Add(' DM_COLOR'); if (dmFields and DM_DUPLEX)<>0 then PrnCaps.Lines.Add(' DM_DUPLEX'); if (dmFields and DM_YRESOLUTION)<>0 then PrnCaps.Lines.Add(' DM_YRESOLUTION'); if (dmFields and DM_TTOPTION)<>0 then PrnCaps.Lines.Add(' DM_TTOPTION'); if (dmFields and DM_COLLATE)<>0 then PrnCaps.Lines.Add(' DM_COLLATE'); if (dmFields and DM_FORMNAME)<>0 then PrnCaps.Lines.Add(' DM_FORMNAME'); if (dmFields and DM_LOGPIXELS)<>0 then PrnCaps.Lines.Add(' DM_LOGPIXELS'); if (dmFields and DM_BITSPERPEL)<>0 then PrnCaps.Lines.Add(' DM_BITSPERPEL'); if (dmFields and DM_PELSWIDTH)<>0 then PrnCaps.Lines.Add(' DM_PELSWIDTH'); if (dmFields and DM_PELSHEIGHT)<>0 then PrnCaps.Lines.Add(' DM_PELSHEIGHT'); if (dmFields and DM_DISPLAYFLAGS)<>0 then PrnCaps.Lines.Add(' DM_DISPLAYFLAGS'); if (dmFields and DM_DISPLAYFREQUENCY)<>0 then PrnCaps.Lines.Add(' DM_DISPLAYFREQUENCY'); if (dmFields and DM_ICMMETHOD)<>0 then PrnCaps.Lines.Add(' DM_ICMMETHOD'); if (dmFields and DM_ICMINTENT)<>0 then PrnCaps.Lines.Add(' DM_ICMINTENT'); if (dmFields and DM_MEDIATYPE)<>0 then PrnCaps.Lines.Add(' DM_MEDIATYPE'); if (dmFields and DM_DITHERTYPE)<>0 then PrnCaps.Lines.Add(' DM_DITHERTYPE'); s:=Format('Orientation (dmOrientation)=%d',[dmOrientation]); if (dmFields and DM_ORIENTATION)=0 then s:=s+NIN else case dmOrientation of DMORIENT_PORTRAIT :s:=s+' (PORTRAIT)'; DMORIENT_LANDSCAPE:s:=s+' (LANDSCAPE)'; else s:=s+' (UNKNOWN)'; end; PrnCaps.Lines.Add(s); s:=Format('Paper size (dmPaperSize)=%d',[dmPaperSize]); if (dmFields and DM_PAPERSIZE)=0 then s:=s+NIN else case dmPaperSize of DMPAPER_LETTER :s:=s+' (Letter, 8 1/2- by 11-inches)'; DMPAPER_LEGAL :s:=s+' (Legal, 8 1/2- by 14-inches)'; DMPAPER_A4 :s:=s+' (A4 Sheet, 210- by 297-millimeters)'; DMPAPER_CSHEET :s:=s+' (C Sheet, 17- by 22-inches)'; DMPAPER_DSHEET :s:=s+' (D Sheet, 22- by 34-inches)'; DMPAPER_ESHEET :s:=s+' (E Sheet, 34- by 44-inches)'; DMPAPER_LETTERSMALL :s:=s+' (Letter Small, 8 1/2- by 11-inches)'; DMPAPER_TABLOID :s:=s+' (Tabloid, 11- by 17-inches)'; DMPAPER_LEDGER :s:=s+' (Ledger, 17- by 11-inches)'; DMPAPER_STATEMENT :s:=s+' (Statement, 5 1/2- by 8 1/2-inches)'; DMPAPER_EXECUTIVE :s:=s+' (Executive, 7 1/4- by 10 1/2-inches)'; DMPAPER_A3 :s:=s+' (A3 sheet, 297- by 420-millimeters)'; DMPAPER_A4SMALL :s:=s+' (A4 small sheet, 210- by 297-millimeters)'; DMPAPER_A5 :s:=s+' (A5 sheet, 148- by 210-millimeters)'; DMPAPER_B4 :s:=s+' (B4 sheet, 250- by 354-millimeters)'; DMPAPER_B5 :s:=s+' (B5 sheet, 182- by 257-millimeter paper)'; DMPAPER_FOLIO :s:=s+' (Folio, 8 1/2- by 13-inch paper)'; DMPAPER_QUARTO :s:=s+' (Quarto, 215- by 275-millimeter paper)'; DMPAPER_10X14 :s:=s+' (10- by 14-inch sheet)'; DMPAPER_11X17 :s:=s+' (11- by 17-inch sheet)'; DMPAPER_NOTE :s:=s+' (Note, 8 1/2- by 11-inches)'; DMPAPER_ENV_9 :s:=s+' (#9 Envelope, 3 7/8- by 8 7/8-inches)'; DMPAPER_ENV_10 :s:=s+' (#10 Envelope, 4 1/8- by 9 1/2-inches)'; DMPAPER_ENV_11 :s:=s+' (#11 Envelope, 4 1/2- by 10 3/8-inches)'; DMPAPER_ENV_12 :s:=s+' (#12 Envelope, 4 3/4- by 11-inches)'; DMPAPER_ENV_14 :s:=s+' (#14 Envelope, 5- by 11 1/2-inches)';
    Mutasd a teljes hozzászólást!
  • Köszi fusinuku sokat segítettél, tiéd a pont!
    Köszi mindenkinek!
    Mutasd a teljes hozzászólást!
Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd