{X=2290,Y=2970 每次运行后打印机就定位到了Letter纸张,甚是郁闷,希望大家指点!!!}
procedure SetPaperSize(X, Y: Integer);
var
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device,Driver,Port,hDMode);
// 获取打印机DevMode 结构的句柄值,存放在hDMode 中
if hDMode<>0 then begin
pDMode := GlobalLock(hDMode);
// 获取指向打印机DevMode 结构的// 指针
if pDMode <> nil then begin
pDMode^.dmPaperSize := 256;
// 如果要将当前打印机纸张变为自定义,dmPaperSize 必须设置成256
pDMode^.dmPaperLength := X;
pDMode^.dmPaperWidth := Y;
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE;
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERLENGTH;
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERWIDTH;
{以上三条语句是对相应的dmFields 成员进行置位。}
ResetDC(Printer.Handle,pDMode^); // 设置打印机设备环境句柄的值
GlobalUnlock(hDMode);
//以下开始打印
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
procedure SetPaperSize(X, Y: Integer);
var
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device,Driver,Port,hDMode);
// 获取打印机DevMode 结构的句柄值,存放在hDMode 中
if hDMode<>0 then begin
pDMode := GlobalLock(hDMode);
// 获取指向打印机DevMode 结构的// 指针
if pDMode <> nil then begin
pDMode^.dmPaperSize := 256;
// 如果要将当前打印机纸张变为自定义,dmPaperSize 必须设置成256
pDMode^.dmPaperLength := X;
pDMode^.dmPaperWidth := Y;
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE;
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERLENGTH;
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERWIDTH;
{以上三条语句是对相应的dmFields 成员进行置位。}
ResetDC(Printer.Handle,pDMode^); // 设置打印机设备环境句柄的值
GlobalUnlock(hDMode);
//以下开始打印
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
这个只能在WIN98中才是这样的,在WINNT/XP/2000以上的系统中自定义纸张的ID不是256,而是一个随机的数。想要在WINNT以上系统中使用自定义纸张,就要用手工添加,或在程序中添加。添加方法请看http://www.tansoo.cn上的“在WIN2000/XP下添加自定义纸张”一文。
同样的x,y,str参数,用A3打印的位置和我用自己定义的纸张打印的位置上下相差5毫米的样子。是不是纸张自定义的时候还要设置它的上边距阿(纸张有上边距的说法吗?)?