如何在程序中自定义打印纸的大小?为何我的这段Code一点作用也没有?请指教?(100分)

G

Great

Unregistered / Unconfirmed
GUEST, unregistred user!
请看我这段代码,我明明把打印纸大小自定义成了1800*960,但用LQ1600K
打时,仍是默认的A4大小.还得要我手动设置,该如何能在程序中用Code
搞定呢?
var
PrintSZ:Boolean;
AD1,AD2,APort:array[0..255] of Char;
DevHandle:THandle;
DevMode:pDeviceMode;
begin

if PrintSZ then

begin

Printer.GetPrinter(AD1,AD2,APort,DevHandle);
if DevHandle = 0 then

begin

Printer.PrinterIndex :=Printer.PrinterIndex ;
Printer.GetPrinter(AD1,AD2,APort,DevHandle);
end;

if DevHandle = 0 then

begin

Application.MessageBox('打印机错误!',AppTitle,MB_OK+MB_ICONQUESTION);
Exit;
end else

begin

DevMode:=GlobalLock(DevHandle);
with DevMode^do

begin

dmFields:=dmFields or DM_PAPERLENGTH;
dmPaperLength:=960;
dmFields:=dmFields or DM_PAPERWIDTH;
dmPaperWidth:=1800;
end;

if PreCheck.Checked then

begin

PrintPreview(True);
//打印
end else

begin

PreparePrint;
Print;
DeleteAllTempFiles;
end;

end;

if Not DevHandle = 0 then
GlobalUnlock(DevHandle);
end;
 
哈哈,DELPHI好象就是没有办法用标准办法解决打印纸尺寸的设置问题,
不过你可以:

1:使用其他打印机设置控件来做,这样的控件在32BIT深度历险上面一堆一堆
的,一般都带源码,非常好用。
2:将98的COMTROL32。OCX因如到程序中来,不过记得换个名,不然DELPHI会
报错说它已经存在,利用这个OCX,好象只要一行语句就可以调出98的标准设置
窗口。
3:去看打印机设置API好了
 
呵呵, devmode有这种调用方法吗???
应该用printer_info_2来调用devmode吧:)
 
自定义打印纸是有些问题,
这方面,ereport,fastreport做的都不错。你可参考一下她们的源码码。
 
不用设置DevMode^.dmPaperSize:=DMPAPER_USER;吗?
 
Win 32 API About DevMode
....
dmPaperSize
Selects the size of the paper to print on. This member can be set to zero if the length and width of the paper are both set by the dmPaperLength and dmPaperWidth members
....
你好象忘了设置dmPaperSize:=0了

记得修改DevMode后似乎应该再执行一下
Printer.PrinterIndex:=Printer.PrinterIndex
 
在执行打印前调用以下函数:
procedure SetPaperSize(X, Y: Integer);
// 这段代码绝对可用。单位是0.1mm
// A4时 Printer.Pagewidth:=1440; A5时 Printer.Pagewidth:=1049;
// B5时 Printer.Pagewidth:=1290; 16K时 Printer.Pagewidth:=1035;
// lq1600宽行打印机这个值宽度最大为42cm左右, 长度大约2m。
{Question:
How can I change the papersize of my print job?
Answer:
One way to change printer settings at the start
of a print job is to change the printer's devicemode
structure.
See: TDEVMODE in the Delphi 1.02 help file or DEVMODE
in the Delphi 2.01 help file for other settings you can
change (providing the print driver supports the change).
The following example, contains code to change the papersize and
the paper bin that is uses:}
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

{Set to legal}
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
{pDMode^.dmPaperSize := DMPAPER_LEGAL;
changed by wulianmin}
pDMode^.dmPaperSize := DMPAPER_FANFOLD_US;
end
else

begin

{Set to custom size}
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperWidth := x {SomeValueInTenthsOfAMillimeter};
pDMode^.dmPaperLength := y {SomeValueInTenthsOfAMillimeter};
end;

{Set the bin to use}
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;

GlobalUnlock(hDMode);
end;

end;

Printer.PrinterIndex := Printer.PrinterIndex;
//以下开始打印
end;
 
多人接受答案了。
 
顶部