设置纸张大小过程:
procedure TMainForm.PSetPageSize(nPageW, nPageH: Integer);
var
  aDevice: array[0..CCHDEVICENAME-1] of Char;
  aDriver: array[0..MAX_PATH-1] of Char;
  aPort: array[0..31] of Char;
  hDevMode: THandle;
  pDevMode: PDeviceMode;
begin
  // 获取打印机DeviceMode的句柄
  Printer.GetPrinter(aDevice, aDriver, aPort, hDevMode);
  if hDevMode <> 0 then
  begin
  // 获取指向DeviceMode的指针
    pDevMode := GlobalLock(hDevMode);
    if pDevMode <> nil then
    begin
      pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERSIZE;
      pDevMode^.dmPaperSize := DMPAPER_USER;
      pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERLENGTH;
      pDevMode^.dmPaperLength := nPageH;
      pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERWIDTH;
      pDevMode^.dmPaperWidth := nPageW;
      ResetDC(Printer.Handle, pDevMode^);
      GlobalUnlock(hDevMode);
    end;
  end;
end;procedure TMainForm.Button2Click(Sender: TObject);
begin
  PSetPageSize(1450,1900);
  with Printer do
  begin
    Orientation :=poPortrait;
    BeginDoc;
    Canvas.TextOut(0,0,打印内容);
    EndDoc;
  end;
end;其打印还是按A4纸来打印,我打印机的型号是Canon LBP-810,
请各位大侠帮我看看,为何设置纸张大小无效?

解决方案 »

  1.   

    http://borland.mblogger.cn/aiirii/posts/3412.aspx
      

  2.   

    程序没有什么问题,但:
    1,win98下的打印控制最方便,win2000较麻烦,但都必须要自定义纸张
    2,打印机最好是针打,还要看针打的型号,
    3,自定义大小有限制,不能太小或太大
    4,进纸长度难控制,一般比设的要长一些
    5,。。太多了,还要看你打印控制的精细程度,精度高的,直接写到打印机
      

  3.   

    建议使用打印控件吧,我就是用REPORT MACHINE打印控件自定义纸张大小的
      

  4.   

    rocedure UpdatePrint(Awidth,Aheight:integer);
    const    CustomFormName = 'ZJ Defined';  function Win95SetForm(PDevMode: PDeviceMode): Boolean;
      begin
        Printer.PrinterIndex := Printer.PrinterIndex;
        PDevMode.dmFields := PDevMode.dmFields or DM_PAPERSIZE;
        PDevMode.dmPaperSize := 256;
        PDevMode.dmFields := PDevMode.dmFields or DM_PAPERWIDTH;
        PDevMode.dmPaperWidth := AWidth;
        PDevMode.dmFields := PDevMode.dmFields or DM_PAPERLENGTH;
        PDevMode.dmPaperLength := AHeight;
        Printer.PrinterIndex := Printer.PrinterIndex;
        Result := True;
      end;  function WinNTSetForm(PDevMode: PDeviceMode;
        Device: PChar; Port: PChar): Boolean;
      var
        hPrinter: THandle;
        pForm: Pointer;
        cbNeeded: DWORD;
        cReturned: DWORD;
        FormInfo1: TFormInfo1;
      begin
        Result := False;
        if OpenPrinter(Device, hPrinter, nil) then
        begin
          pForm := nil;
          EnumForms(hPrinter, 1, pForm, 0, cbNeeded, cReturned);
          GetMem(pForm, cbNeeded); //取pForm的大小并分配内存
          try
            if EnumForms(hPrinter, 1, pForm, cbNeeded, cbNeeded, cReturned) then
            begin
              if DeleteForm(hPrinter, PChar(CustomFormName)) then
                Dec(cReturned); //删除旧的Form
              with FormInfo1 do
              begin
                Flags := 0;
                pName := PChar(CustomFormName);
                Size.cx := AWidth * 100;
                Size.cy := AHeight * 100;
                with ImageAbleArea do
                begin
                  Left := 0;
                  Top := 0;
                  Right := Size.cx;
                  Bottom := Size.cy;
                end;
              end;
              if AddForm(hPrinter, 1, @FormInfo1) then
              begin
                Printer.PrinterIndex := Printer.PrinterIndex;
                PDevMode.dmFields := PDevMode.dmFields or DM_PAPERSIZE;
                PDevMode.dmPaperSize := cReturned + 1;
                Printer.PrinterIndex := Printer.PrinterIndex;
                Result := True;
              end;
            end;
          finally
            FreeMem(pForm);
          end;
        end;
      end;
    var
      Device, Driver, Port: array[0..127] of char;
      hDevMode: THandle;
      PDevMode: PDeviceMode;
    begin
      Printer.GetPrinter(Device, Driver, Port, hDevMode);
      if hDevMode <> 0 then
      begin
        PDevMode := GlobalLock(hDevMode);
        try
          if (Win32Platform = VER_PLATFORM_WIN32s) or
            (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) then
            Win95SetForm(PDevMode)
          else if Win32Platform = VER_PLATFORM_WIN32_NT then
            WinNTSetForm(PDevMode, Device, Port);
        finally
          GlobalUnlock(hDevMode);
        end;
      end
    end;
      

  5.   

    我现在也在做和cherrylin类似的工作,需要把打印纸设置成为20.1*10.2cm的,但现在只能通过对话框新建一页面来实现,参考网上的一些函数,添加在程序中却没有效果,郁闷中啊。不知有哪位高手成功了啊,可否给出代码?!