如何取得LPT端口打印机支持的 最小和最大页面大小

由于工作需要,经朋友 (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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值