//这个函数能够添加你要的任何纸型。它首先检查你添加的纸型是否存在,不存在就添加,返回值为纸型的索引号
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、通过索引号选中自定义纸型即可:
QuickRep1.Page.PaperSize := AddCustomPaperSize('huhongru 同志的纸型', 1024, 768);