Mega Code Archive

 
Categories / Delphi / Printing
 

Getting printer information

Title: Getting printer information Question: I want to get details of the printer's paper and margins, but GetDeviceCaps does not work reliably if I am not already printing. Answer: From Steve Schafer: PaperWidth := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALWIDTH); PaperHeight := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALHEIGHT); PrintableWidth := GetDeviceCaps(Printer.Canvas.Handle, HORZRES); PrintableHeight := GetDeviceCaps(Printer.Canvas.Handle, VERTRES); LeftMargin := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALOFFSETX); TopMargin := GetDeviceCaps(Printer.Canvas.Handle, PHYSICALOFFSETY); RightMargin := PaperWidth - PrintableWidth - LeftMargin; BottomMargin := PaperHeight - PrintableHeight - TopMargin; You can't access Printer.Canvas.Handle without initiating a print job; you can instead use CreateIC to get a handle to an information context that you can pass to GetDeviceCaps: var Device, Driver, Port: array[0..80] of Char; DevMode: THandle; IC: THandle; begin Printer.GetPrinter(Device, Driver, Port, DevMode); Printer.SetPrinter(Device, Driver, Port, 0); Printer.GetPrinter(Device, Driver, Port, DevMode); IC := CreateIC(Driver, Device, Port, nil); ... PaperWidth := GetDeviceCaps(IC, PHYSICALWIDTH); ... DeleteDC(IC); end;