unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
Windows, Printers, Win32Printers; // Vital for Windows printing subsystems
type
TForm1 = class(TForm)
BtnGetCaps: TButton;
MemoLog: TMemo;
procedure BtnGetCapsClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.BtnGetCapsClick(Sender: TObject);
var
PrnHandle: HDC;
PageWidth, PageHeight: Integer;
PrintWidth, PrintHeight: Integer;
OffsetX, OffsetY: Integer;
DpiX, DpiY: Integer;
begin
MemoLog.Clear;
// Verify that at least one printer is installed on Windows 7
if Printer.Printers.Count = 0 then
begin
MemoLog.Lines.Add('No printers found on this system.');
Exit;
end;
try
MemoLog.Lines.Add('Target Printer: ' + Printer.Printers[Printer.PrinterIndex]);
{
We invoke Refresh to ensure a valid device context handle (Handle)
is created internally by the Lazarus Win32 printing subsystem.
}
Printer.Refresh;
PrnHandle := Printer.Handle;
if PrnHandle = 0 then
begin
MemoLog.Lines.Add('Failed to retrieve Printer Device Context (DC) Handle.');
Exit;
end;
// 1. Fetch Dots Per Inch (DPI)
DpiX := GetDeviceCaps(PrnHandle, LOGPIXELSX);
DpiY := GetDeviceCaps(PrnHandle, LOGPIXELSY);
// 2. Fetch overall physical sheet dimensions (including unprintable margins)
PageWidth := GetDeviceCaps(PrnHandle, PHYSICALWIDTH);
PageHeight := GetDeviceCaps(PrnHandle, PHYSICALHEIGHT);
// 3. Fetch exact printable area sizes
PrintWidth := GetDeviceCaps(PrnHandle, HORZRES);
PrintHeight := GetDeviceCaps(PrnHandle, VERTRES);
// 4. Fetch the unprintable hardware margins (Offsets)
OffsetX := GetDeviceCaps(PrnHandle, PHYSICALOFFSETX);
OffsetY := GetDeviceCaps(PrnHandle, PHYSICALOFFSETY);
// Format output to UI
MemoLog.Lines.Add(Format('Resolution: %d x %d DPI', [DpiX, DpiY]));
MemoLog.Lines.Add(Format('Physical Paper Size: %d x %d pixels', [PageWidth, PageHeight]));
MemoLog.Lines.Add(Format('Printable Area Size: %d x %d pixels', [PrintWidth, PrintHeight]));
MemoLog.Lines.Add(Format('Hardware Margins (Left/Top): %d x %d pixels', [OffsetX, OffsetY]));
// Convert to millimeters for human-readable outputs
MemoLog.Lines.Add(Format('Physical Size (mm): %.1f x %.1f mm',
[(PageWidth / DpiX) * 25.4, (PageHeight / DpiY) * 25.4]));
except
on E: Exception do
MemoLog.Lines.Add('Error retrieving capabilities: ' + E.Message);
end;
end;
end.