以下的获取和设置打印纸的长宽的方法为什么在打印1620*1040的票据时总是走A4的长度? (100分)

  • 主题发起人 主题发起人 lccc
  • 开始时间 开始时间
L

lccc

Unregistered / Unconfirmed
GUEST, unregistred user!
以下的获取和设置打印纸的长宽的方法为什么
在打印1620*1040的票据时总是走A4的长度?
unit GetAndSetPaperSize;
interface
uses Windows, Forms;
type
TDeviceChar=array[0..255] of Char;
TPaperSize=record //单位:0.1mm
Width:Integer;
Height:Integer;
end;

//获取打印设备
procedure GetPrinterDevice(var ADevice,ADriver,APort:TDeviceChar;
var DeviceHandle:THandle;var DevMode:PDeviceMode);
//锁定打印 设备
procedure LockPrinterDevice(var DeviceHandle:THandle;var DevMode:PDeviceMode);
//解锁打印设备
procedure UnLockPrinterDevice(var DeviceHandle:THandle);
//设置打印机的纸张
procedure SetPaperSize(var ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;DevMode:PDeviceMode;const PaperSize:TPaperSize);
//获取打印机的纸张设置
function GetPaperSize(var ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;DevMode:PDeviceMode):TPaperSize;
implementation
uses Sysutils, Printers;
procedure GetPrinterDevice(var ADevice,ADriver,APort:TDeviceChar;
var DeviceHandle:THandle;var 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
begin
MessageBox(Screen.ActiveForm.Handle,'无法初始化打印机。','打印机错误',MB_OK+MB_ICONERROR);
Abort;
end;
end;

procedure LockPrinterDevice(var DeviceHandle:THandle;var DevMode:PDeviceMode);
begin
try
DevMode:=GlobalLock(DeviceHandle);
except
MessageBox(Screen.ActiveForm.Handle,'无法锁定打印机设备句柄。','打印机错误',MB_OK+MB_ICONERROR);
Abort;
end;
end;

procedure UnLockPrinterDevice(var DeviceHandle:THandle);
begin
if DeviceHandle<>0 then
GlobalUnLock(DeviceHandle);
end;

function GetPaperSize(var ADevice,ADriver,APort:TDeviceChar;//设置打印机的纸张
DeviceHandle:THandle;DevMode:PDeviceMode):TPaperSize;
begin
try
DevMode^.dmFields:=DevMode^.dmFields or DM_PAPERWIDTH;//
Result.Width:=DevMode^.dmPaperWidth;
DevMode^.dmFields:=DevMode^.dmFields or DM_PAPERLENGTH;//
Result.Height:=DevMode^.dmPaperLength;
except
MessageBox(Screen.ActiveForm.Handle,'无法取得打印机的纸张设置。','打印机错误',MB_OK+MB_ICONERROR);
Abort;
end;
end;

procedure SetPaperSize(var ADevice,ADriver,APort:TDeviceChar;//设置打印机的纸张
DeviceHandle:THandle;DevMode:PDeviceMode;const PaperSize:TPaperSize);
begin
try
DevMode^.dmFields:=DevMode^.dmFields or DM_PAPERWIDTH;//
DevMode^.dmPaperWidth:=PaperSize.Width;
DevMode^.dmFields:=DevMode^.dmFields or DM_PAPERLENGTH;
DevMode^.dmPaperLength:=PaperSize.Height;
except
MessageBox(Screen.ActiveForm.Handle,'无法设置打印机的纸张。','打印机错误',MB_OK+MB_ICONERROR);
Abort;
end;
end;

end.

//以下是程序中票据打印时的过程:
procedure TfrmBillPrint.PrintBillwithCanvas;//采用画布方式打印票据
var
PaperSize:TPaperSize;//旧的纸张设置
OldOP:TPrinterOration;//旧的打印方向
ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;
DevMode:PDeviceMode
begin
//保存旧的打印方向
OldOP:=Printer.打印方向属性
GetPrinterDevice(ADevice,ADriver,APort,DeviceHandle,DevMode);//获取打印设备
LockPrinterDevice(DeviceHandle,DevMode);
//保存旧的纸张 格式
PaperSize:=GetPaperSize(ADevice,ADriver,APort,DeviceHandle,DevMode);
//将纸张设置为票据的格式
SetPaperSize(DeviceHandle,DevMode,BillSize);//这里BillSize.Width=1620,BillSize.Height=1040
with Printerdo
try
begin
Doc;
....//打印工作
finally
UnLockPrinterDevice(DeviceHandle);
// 恢复原来的打印方向
//恢复原来的纸张设置
SetPaperSize(DeviceHandle,DevMode,PaperSize)
end;
end;
 
打印机支持自定义不?
 
要使用自定义纸张设置,要把dmPaperSize设置为零
procedure SetPaperSize(var ADevice,ADriver,APort:TDeviceChar;//设置打印机的纸张
DeviceHandle:THandle;DevMode:PDeviceMode;const PaperSize:TPaperSize);
begin
try
DevMode^.dmFields:=DevMode^.dmFields or DM_PAPERWIDTH or DM_PAPERLENGTH or DM_PAPERSIZE;
DevMode^.dmPaperSize:=0;

DevMode^.dmPaperWidth:=PaperSize.Width;
DevMode^.dmPaperLength:=PaperSize.Height;
except
MessageBox(Screen.ActiveForm.Handle,'无法设置打印机的纸张。','打印机错误',MB_OK+MB_ICONERROR);
Abort;
end;
end;

这样才行。
当然要恢复纸张设置,还得恢复原来的dmPaperSize值
 
问题没有解决!,请继续!
 
DevMode^.dmPaperSize:=DMPAPER_USER;
建议去看看windows.pas
里面放了全部delphi的打印的页面参数,其中最后一个就是 DMPAPER_USER = $100
 
你现在只是设定了输出页面的宽和高,而页面的大小仍然是A4
就好像你给他一张纸,纸上画了个矩形,让打印机在矩形里输出,然后又默认整张纸的大小是A4
 
我以前碰到过类似的,把纸张设置成1100*1080
没设dmPaperSize为0时,总是出A4的长度,设成0后就可以了
你的怎么不行就不知道了,是什么操作系统?
 
To : ggb199 ,你的比喻实在是恰当!谢谢!
 
后退
顶部