如何在程序中改变打印机纸张大小?(100分)

  • 主题发起人 主题发起人 zhao_bai_ming
  • 开始时间 开始时间
&nbsp;Printer.PageHeight<br>&nbsp; Printer.PageWidth<br>
 
建議看一下&lt;&lt;delphi4編程大全&gt;&gt;一書
 
書名: delphi4編程大全
 
是開個"form" setup,還是其他的思路?
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Printers;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Device:Array[0..256] of Char;<br>&nbsp; Driver:Array[0..256] of Char;<br>&nbsp; Port:Array[0..32] of Char;<br>&nbsp; hDMode:THandle;<br>&nbsp; pDMode:PDevMode;<br>begin<br>&nbsp; Printer.GetPrinter(Device,Driver,Port,hDMode);<br>&nbsp; if hDMode&lt;&gt;0 then<br>&nbsp; &nbsp; pDMode:=GLobalLock(hDMode);<br>&nbsp; if pDMode&lt;&gt;nil then<br>&nbsp; begin<br>&nbsp; &nbsp; pDMode^.dmPaperSize:=256;<br>&nbsp; &nbsp; pDMode^.dmPaperLength:=1140;<br>&nbsp; &nbsp; pDMode^.dmPaperWidth:=1900;<br>&nbsp; &nbsp; pDMode^.dmFields:=pDMode^.dmFields or DM_PAPERSIZE;<br>&nbsp; &nbsp; pDMode^.dmFields:=pDMode^.dmFields or DM_PAPERLENGTH;<br>&nbsp; &nbsp; pDMode^.dmFields:=pDMode^.dmFields or DM_PAPERWIDTH;<br>&nbsp; end;<br>&nbsp; ResetDC(Printer.Handle,pDMode^);<br>&nbsp; GLobalUnLock(hDMode);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; With Printer do<br>&nbsp; begin<br>&nbsp; &nbsp; BeginDoc;<br>&nbsp; &nbsp; Canvas.TextOut(10,10,'fkgfjhg');<br>&nbsp; &nbsp; EndDoc;<br>&nbsp; end;<br>end;<br><br>end.
 
最好是在procedure TForm1.Button1Click(Sender: TObject);<br>开始处加一行: printer.printerindex:=printer.printerindex;
 
既然问题的分类是:Windows API,我就用API来解决:<br><br>// 纸张改为 A4<br>uses WinSpool;<br>procedure TFrom1.PreparePrinter;<br>var<br>&nbsp; hPrinter: THandle;<br>&nbsp; PrtInfo: PPrinterInfo1;<br>&nbsp; DevMode: PDeviceMode;<br>&nbsp; pcbNeeded, PDevModeBytes: DWORD;<br>begin<br>&nbsp; OpenPrinter('Epson LQ-1600K', hPrinter, nil);<br>&nbsp; GetMem(PrtInfo, 1024);<br>&nbsp; GetPrinter(hPrinter, 1, PrtInfo, 1024, @pcbNeeded);<br>&nbsp; PDevModeBytes := DocumentProperties(Handle, hPrinter, PrtInfo^.pDescription, DevMode^, DevMode^, 0);<br>&nbsp; GetMem(DevMode, pDevModeBytes);<br>&nbsp; DocumentPropertied(Handle, hPrinter, PrtInfo^.PDescription, DevMode^, DevMode^, DM_OOT_BUFFER);<br>&nbsp; DevMode^.dmPaperSize := DMPAPER_A4;<br>&nbsp; DevMode^.dmFields := DevMode^.dmFields or DM_PAPERSIZE;<br>&nbsp; DocumentProperties(Handle, hPrinter, PrtInfo^.PDescription, DevMode^, DevMode^, DM_IN_BUFFER);<br>&nbsp; FreeMem(PrtInfo);<br>&nbsp; FreeMem(DevMode);<br>end;
 
多人接受答案了。
 
后退
顶部