怎样设置打印方向?(100分)

D

difilwy

Unregistered / Unconfirmed
GUEST, unregistred user!
用tprinter类的getprinter和setprinter的时候,怎么老提示错误(setprinter)?
我察看帮助文件,说什么不能直接用set命令来设置,应该用createdc,API我可是
一窍不通,请教各位大虾,最好能给可以直接使用的源码,谢谢!
 
注意先引用Printers单元。
procedure TForm1.Button1Click(Sender: TObject);
var
ADevice, ADriver, APort: array[0..255] of Char;
DeviceHandle: THandle;
DevMode: PDeviceMode;
begin

//初始化打印机
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
if DeviceHandle = 0 then

begin

Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
end;

if DeviceHandle = 0 then

Raise Exception.Create('不能初始化打印机!')
else

DevMode := GlobalLock(DeviceHandle);
if DeviceHandle <> 0 then

begin

with DevMode^do

begin

dmFields := dmFields or DM_ORIENTATION;
//在下面这里设置打印方向
dmOrientation := DMORIENT_LANDSCAPE;
//DMORIENT_PORTRAIT
Printer.SetPrinter(ADevice, ADriver, APort, DeviceHandle);
GlobalUnLock(DeviceHandle);
end;

end;


PrinterSetupDialog1.Execute;
//验证一下设置是否成功了
end;

 
利用TPrinter对象的Orientation特性简单明了
在uses先引用Printers单元
printer.Orientation:=polandscape;
//横向
printer.Orientation:=poportrait;
//纵向
 
不错,以前面那种做法来解决这个问题确实是舍近求远。
前面那种做法的优点在于可以调整纸张尺寸、设置颜色、双面打印等Printer对象方法不能
做到的功能。
例如设置纸张尺寸,把下面两行代码:
dmFields := dmFields or DM_ORIENTATION;
//在下面这里设置打印方向
dmOrientation := DMORIENT_LANDSCAPE;
//DMORIENT_PORTRAIT
替换为下面的代码,就是把纸张设置为A3:
dmFields := dmFields or DM_PAPERSIZE;
dmPaperSize := DMPAPER_A3;
//如果打印机不支持此种纸张规格,设置值无效
而替换为下面的代码,就是把纸张设置为长32厘米宽24厘米的自定义纸张:
dmFields := dmFields or DM_PAPERSIZE or DM_PAPERLENGTH or DM_PAPERWIDTH;
dmPaperSize := DMPAPER_USER;
dmPaperLength := 3200;
dmPaperWidth := 2400;
//如果打印机不支持如此大的纸张,设置值无效


 
多人接受答案了。
 
问题没有完全解决。
我现在是用的f1book中的F1BookNew1.FilePrintSetupDlg;来设置f1book预览中的方向的
用上面的方法却不能改变F1BookNew1.FilePrintSetupDlg的值,每次使用时它还是默认
为纵向打印
为什么同一个打印机,会有两种方向同时存在?
 

Similar threads

D
回复
0
查看
677
DelphiTeacher的专栏
D
D
回复
0
查看
690
DelphiTeacher的专栏
D
D
回复
0
查看
708
DelphiTeacher的专栏
D
顶部