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 DevModeDeviceMode);
//锁定打印 设备
procedure LockPrinterDevice(var DeviceHandle:THandle;var DevModeDeviceMode);
//解锁打印设备
procedure UnLockPrinterDevice(var DeviceHandle:THandle);
//设置打印机的纸张
procedure SetPaperSize(var ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;DevModeDeviceMode;const PaperSize:TPaperSize);
//获取打印机的纸张设置
function GetPaperSize(var ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;DevModeDeviceMode):TPaperSize;
implementation
uses Sysutils, Printers;
procedure GetPrinterDevice(var ADevice,ADriver,APort:TDeviceChar;
var DeviceHandle:THandle;var DevModeDeviceMode);//获取打印机的纸张设置
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 DevModeDeviceMode);
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;DevModeDeviceMode):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;DevModeDeviceMode;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;
DevModeDeviceMode
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;
在打印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 DevModeDeviceMode);
//锁定打印 设备
procedure LockPrinterDevice(var DeviceHandle:THandle;var DevModeDeviceMode);
//解锁打印设备
procedure UnLockPrinterDevice(var DeviceHandle:THandle);
//设置打印机的纸张
procedure SetPaperSize(var ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;DevModeDeviceMode;const PaperSize:TPaperSize);
//获取打印机的纸张设置
function GetPaperSize(var ADevice,ADriver,APort:TDeviceChar;
DeviceHandle:THandle;DevModeDeviceMode):TPaperSize;
implementation
uses Sysutils, Printers;
procedure GetPrinterDevice(var ADevice,ADriver,APort:TDeviceChar;
var DeviceHandle:THandle;var DevModeDeviceMode);//获取打印机的纸张设置
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 DevModeDeviceMode);
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;DevModeDeviceMode):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;DevModeDeviceMode;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;
DevModeDeviceMode
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;