我是这样写的:
nPaperWidth :=  GetDeviceCaps(Printer.Handle, PHYSICALWIDTH) div GetDeviceCaps 
   (Printer.Handle, LOGPIXELSX) * 254;
nPaperHeight := GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT) div GetDeviceCaps
   (Printer.Handle, LOGPIXELSY) * 254;
结果取到的是2032×2794,跟我想要取的A4纸的2100×2970差一点,正确的应该是怎么做?
因为我要做表格的打印缩放,所以必须要取得纸张的准确大小。
谁知道怎么做?马上结贴。

解决方案 »

  1.   

    打印时,改变纸张大小:在执行打印前调用以下函数:
    procedure SetPaperSize(X, Y: Integer);
    // 单位是0.1mm
    // A4时 Printer.Pagewidth:=1440;  A5时 Printer.Pagewidth:=1049;
    // B5时 Printer.Pagewidth:=1290;  16K时 Printer.Pagewidth:=1035;
    // lq1600宽行打印机这个值宽度最大为42cm左右, 长度大约2m。
    //改变devicemode结构
    var
      Device: array[0..255] of char;
      Driver: array[0..255] of char;
      Port: array[0..255] of char;
      hDMode: THandle;
      PDMode: PDEVMODE;
    begin
      Printer.PrinterIndex := Printer.PrinterIndex;
      Printer.GetPrinter(Device, Driver, Port, hDMode);
      if hDMode <> 0 then
      begin
        pDMode := GlobalLock(hDMode);
        if pDMode <> nil then
        begin
          if (x = 0) or (y = 0) then
          begin
            {Set to legal}
            pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
            {pDMode^.dmPaperSize := DMPAPER_LEGAL; changed by wulianmin}
            pDMode^.dmPaperSize := DMPAPER_FANFOLD_US;
          end
          else
          begin
            {Set to custom size}
            pDMode^.dmFields := pDMode^.dmFields or
              DM_PAPERSIZE or
              DM_PAPERWIDTH or
              DM_PAPERLENGTH;
            pDMode^.dmPaperSize := DMPAPER_USER;
            pDMode^.dmPaperWidth := x {SomeValueInTenthsOfAMillimeter};
            pDMode^.dmPaperLength := y {SomeValueInTenthsOfAMillimeter};
          end;
          {设定纸张来源}
          pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
          pDMode^.dmDefaultSource := DMBIN_MANUAL;      GlobalUnlock(hDMode);
        end;
      end;
      Printer.PrinterIndex := Printer.PrinterIndex;
      //以下开始打印
      Printer.BeginDoc;
      Printer.Canvas.TextOut(100,100, 'Test 1');
      Printer.EndDoc;
    end;
      

  2.   

    Escape( printer.handle, GETPHYSPAGESIZE, 0, nil, @physsize );可取得纸张的实际尺寸(点数,与打印机分辨率有关,把它转为以Twips为单位就是实际尺寸)
      

  3.   

    pixelsperinchx := GetDeviceCaps( printer.handle, LOGPIXELSX );
        Longint(printer.pagewidth) * 1440) div pixelsperinchx
    可得到打印机可打印的范围尺寸(已转为Twips)。