1、这是我过去给别人写的一个函数,能够添加你要的任何纸型。它首先检查你添加的纸型是否存在,不存在就添加,返回值为纸型的索引号:
uses WinSpool, Printers;
//pFormName: 自定义纸型的名称,不能与已有的重名;
nPaperWidth, nPaperLength: 纸宽和纸高;
function AddCustomPaperSize(pFormName: PChar;
nPaperWidth, nPaperLength: Longint): Longint;
var
phPrinter, dwNeeded, dwReturned, i: LongWord;
pBuf: PChar;
pFormInfo: PFormInfo1A;
begin
Result := -1;
if OpenPrinter(PChar(Printer.Printers[0]), phPrinter, nil) then
begin
EnumForms(phPrinter, 1, nil, 0, dwNeeded, dwReturned);
pBuf := AllocMem(dwNeeded);
EnumForms(phPrinter, 1, pBuf, dwNeeded, dwNeeded, dwReturned);
for i := 0 to dwReturned - 1do
if PFormInfo1A(pBuf + i*SizeOf(_FORM_INFO_1A)).pName = String(pFormName) then
begin
Result := i;
Break;
end;
FreeMem(pBuf);
if Result < 0 then
begin
New(pFormInfo);
with pFormInfo^do
begin
Flags := FORM_USER;
pName := pFormName;
Size.cx := nPaperWidth;
Size.cy := nPaperLength;
ImageableArea.Left := 0;
ImageableArea.Top := 0;
ImageableArea.Right := Size.cx;
ImageableArea.Bottom := Size.cy;
end;
AddForm(phPrinter, 1, pFormInfo);
Dispose(pFormInfo);
Result := dwReturned;
end;
end;
ClosePrinter(phPrinter);
end;
2、通过索引号选中自定义纸型即可:
function SetPaperSize(const Index: Longint): Boolean;
var
Device, Driver, Port: array[0..255] of Char;
hDMode: LongWord;
pDMode: PDeviceModeA;
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
Result := hDMode <> 0;
if Result then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
with pDMode^do
begin
dmFields := DM_PAPERSIZE;
dmPaperSize := Index;
......
end;
GlobalUnlock(hDMode);
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
3、以上函数跟你使用什么类型的报表无关。添加纸型是最重要的,至于如何选择纸型由你了,越简单越好。如果你用了报表,可以简化第二步,例如写成 Report.PageSetup.PaperSize := Index;
即可,不必写那么多。
4、能用请结贴,不能用说一声。