注意先引用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;