由于工作需要,经朋友 (DS,FT)指点,先贴工作中代码,至于使用USB端口打印机,日后再说。
TPrinterSett = record
PrinterName: string;
MinPaperSize: TPoint;
MaxPaperSize: TPoint;
end;
private
FPrnSett: TPrinterSett;
uses
Printers, WinSpool;
function GetMinMaxPaperSize: Boolean;
var
R, dwNeeded: DWORD;
PrnName, PortName: string;
hPrn: THandle;
DevMode: TDeviceMode;
pPrnInfo: PPrinterInfo2;
begin
Result := False;
if Printer.PrinterIndex < 0 then Exit;
PrnName := Printer.Printers.Strings[Printer.PrinterIndex];
if not OpenPrinter(PChar(PrnName), hPrn, nil) then Exit;
try
FPrnSett.PrinterName := PrnName;
if not GetPrinter(hPrn, 2, nil, 0, @dwNeeded) then
begin
pPrnInfo := AllocMem(dwNeeded);
try
if GetPrinter(hPrn, 2, pPrnInfo, dwNeeded, @dwNeeded) then
begin
PortName := pPrnInfo.pPortName;
if Pos('LPT', PortName) = 0 then Exit;
end else begin
Exit;
end;
finally
FreeMem(pPrnInfo);
end;
end;
R := DeviceCapabilities(
PChar(PrnName), PChar(PortName), DC_MINEXTENT, nil, @DevMode);
if R > 0 then
begin
with FPrnSett do
begin
MinPaperSize.x := LoWord(R);
MinPaperSize.y := HiWord(R);
{
ShowMessage('Min Paper Width: ' + IntToStr(MinPaperSize.x) + #13 +
'Min Paper Length: ' + IntToStr(MinPaperSize.y));
}
end;
end else begin
Exit;
end;
R := DeviceCapabilities(
PChar(PrnName), PChar(PortName), DC_MAXEXTENT, nil, @DevMode);
if R > 0 then
begin
with FPrnSett do
begin
MaxPaperSize.x := LoWord(R);
MaxPaperSize.y := HiWord(R);
{
ShowMessage('Max Paper Width: ' + IntToStr(MaxPaperSize.x) + #13 +
'Max Paper Length: ' + IntToStr(MaxPaperSize.y));
}
end;
end else begin
Exit;
end;
Result := True;
finally
ClosePrinter(hPrn);
end;
end;