如题。我找了一段设置打印机纸张大小的代码,为什么不管用?procedure SetPrinterSize(iWidth, iLength: Integer);  // 单位 毫米
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
      {Set to legal}
      pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
      pDMode^.dmPaperSize := DMPAPER_LEGAL;     {Set to custom size}
      pDMode^.dmFields := pDMode^.dmFields or
                          DM_PAPERSIZE or
                          DM_PAPERWIDTH or
                          DM_PAPERLENGTH;
      pDMode^.dmPaperSize := DMPAPER_USER;
      pDMode^.dmPaperWidth :=iWidth*10;
      pDMode^.dmPaperLength :=iLength*10;
      pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
      pDMode^.dmDefaultSource := DMBIN_MANUAL;
          GlobalUnlock(hDMode);
      end;
    end;
  Printer.PrinterIndex := Printer.PrinterIndex;
end;

解决方案 »

  1.   

    用Delphi举个例子吧,以下程序将打印机纸张设为:114mm*190mm:procedure PreparePrinter;varaDevice: 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 thenbegin// 获取指向DeviceMode的指针pDevMode := GlobalLock(hDevMode);if pDevMode <> nil thenbeginpDevMode^.dmPaperSize := DMPAPER_USER;pDevMode^.dmPaperLength := 1140;pDevMode^.dmPaperWidth := 1900;pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERSIZE;pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERLENGTH;pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERWIDTH;ResetDC(Printer.Handle, pDevMode^);GlobalUnlock(hDevMode);end;end;end;用修改DeviceMode的方法的话,只是改变你程序中的打印机设置,不会影响其他程序打印的。
     
                        -------------------出自超级猛料2003版
      

  2.   

    虽说很早就看过这种方法。
    一直用QR,Rave。
    这个办法一直也没用过,我也去玩玩。
      

  3.   

    procedure PaperSizeSet(iaWith,iaLength:Integer);
    var
      ADevice : array[0..255] of char;
      ADriver : array[0..255] of char;
      APort   : array[0..255] of char;
      hDevHandle : THandle;
      PDevMode : PDeviceMode; //A Pointer to a TDeviceMode structure
    begin
      Printer.PrinterIndex:=Printer.PrinterIndex;
      // GetPrinter() 首先获得TPrinter的DeviceMode结构的句柄
      Printer.GetPrinter(ADevice, ADriver, APort, hDevHandle);
      //如果句柄为0,表示打印机没有装载
      if hDevHandle = 0 then
      begin
        Printer.PrinterIndex:=Printer.PrinterIndex;
        Printer.GetPrinter(ADevice, ADriver, APort, hDevHandle);
      end;
      //如果句柄还是为0,表示有错误发生。否则,就调用
      if hDevHandle = 0 then
        raise Exception.Create('不能初始化打印结构,请察看打印机是否已加载')
      else
       if hDevHandle<>0 then
       begin
         PDevMode:=GlobalLock(hDevHandle);//GlobalLock()获取TDeviceMode结构的指针
         if PDevMode <> nil then
         try
               //Set to legal
           PDevMode^.dmFields := PDevMode^.dmFields or dm_PaperSize;
           PDevMode^.dmPaperSize := DMPAPER_LEGAL;
              //Set to custom size  //设置定制的大小
           PDevMode^.dmFields := PDevMode^.dmFields or
                                 DM_PAPERSIZE or
                                 DM_PAPERWIDTH or
                                 DM_PAPERLENGTH ;
           PDevMode^.dmPaperSize :=DMPAPER_USER;
           PDevMode^.dmPaperWidth :=iaWith;    //SomeValueIn Tenths Of A Millimeter;
           PDevMode^.dmPaperLength :=iaLength;   //SomeValueInTenthsOfAMillimeter;
    {         //Set the bin to use
           PDevMode^.dmFields := PDevMode^.dmFields or DMBIN_MANUAL;
           PDevMode^.dmDefaultSource := DMBIN_MANUAL;     }
         finally
           GlobalUnlock(hDevHandle);
         end;
       end
       else
         raise Exception.Create('不能设置打印纸张大小');
      Printer.PrinterIndex := Printer.PrinterIndex;
    end;