// 在执行打印前调用以下函数:
// x,y:纸张尺寸,单位是0.1mm
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);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
if (x = 0) or (y = 0) then
begin
{设置合法的纸张大小}
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
pDMode^.dmPaperSize := DMPAPER_FANFOLD_US; // 合法的纸张大小标示
end
else
begin
{设置用户自定义纸张}
pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER; // 设置为用户自定义纸张标示
pDMode^.dmPaperWidth := x; // 纸张宽度
pDMode^.dmPaperLength := y; // 纸张长度
end;
{设定纸张来源}
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
end;
---------------------------------------
这样吧,用Delphi举个例子吧,以下程序将打印机纸张设为:114mm*190mm:
procedure PreparePrinter;
var
aDevice: array[0..CCHDEVICENAME-1] of Char;
aDriver: array[0..MAX_PATH-1] of Char;
aPort: array[0..31] of Char;
hDevMode: THandle;
pDevMode: PDeviceMode;
begin
// 获取打印机DeviceMode的句柄
Printer.GetPrinter(aDevice, aDriver, aPort, hDevMode);
if hDevMode <> 0 then
begin
// 获取指向DeviceMode的指针
pDevMode := GlobalLock(hDevMode);
if pDevMode <> nil then
begin
pDevMode^.dmPaperSize := DMPAPER_USER;
pDevMode^.dmPaperLength := 1140;
pDevMode^.dmPaperWidth := 1900;
pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERSIZE;
pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERLENGTH;
pDevMode^.dmFields := pDevMode^.dmFields or DM_PAPERWIDTH;
ResetDC(Printer.Handle, pDevMode^);
GlobalUnlock(hDevMode);
end;
end;
end;
用修改DeviceMode的方法的话,只是改变你程序中的打印机设置,不会影响其他程序打印的。如果要永久修改,则可以 直接修改 registry 。打开你要修改的打印机的 key( 在 HKEY_LOCAL_MACHINE\System\CurrentControlSet\control\Print\Printers\ 打印机名\ ) 把上例中的 DevMode 结构添入 Default DevMode 键值中