// uses Printers, WinSpool;
//如果自定义纸张类型名称为 pFormName 则选取
//否则添加
//改变当前的打印纸张设置
// pFormName 纸张类型名称 nPaperWidth 纸张宽度 nPaperLength 纸张高度
procedure MySetPaper(pFormName: PChar;
nPaperWidth, nPaperLength: Longint);
stdcall;
var
FormInfos : Array [1..1024] of Form_Info_1;
cbNeeded, cReturned ,neededsize: DWORD;
i,j:integer;
ppo
RINTER_INFO_2;
dev
DeviceMode;
hPrinter: THandle;
FormInfo: TFormInfo1;
PaperSize: TSize;
PaperRect: TRect;
begin
if OpenPrinter(PChar(Printer.Printers[0]), hPrinter, nil) then
begin
EnumForms(hPrinter, 1, Nil, 0, cbNeeded, cReturned);
EnumForms(hPrinter, 1, @FormInfos, cbNeeded, cbNeeded, cReturned );
j := -1;
for i := 1 to cReturned do
//通过轮寻找到 pFormName 的纸张的纸张类型排序号
begin
if string(FormInfos
.pName) = string(pFormName) then
begin
j:=i;
Break;
end;
end;
if j<0 then
//如无 pFormName 纸张则添加
begin
FormInfo.Flags := FORM_USER;
FormInfo.pName := pFormName;
PaperSize.cx := nPaperWidth*100;
PaperSize.cy := nPaperLength*100;
PaperRect.Left := 0;
PaperRect.Top := 0;
PaperRect.Right := nPaperWidth*100;
PaperRect.Bottom := nPaperLength*100;
//纸张的边距
FormInfo.Size := PaperSize;
FormInfo.ImageableArea := PaperRect;
AddForm(hPrinter, 1, @FormInfo);
j := cReturned+1;
end;
GetPrinter(hPrinter, 2, Nil, 0, @NeededSize);
GetPrinter(hPrinter, 2, @ppo, NeededSize, @NeededSize);
dev := ppo.pDevMode;
dev.dmPaperSize := j;
ppo.pDevMode := dev;
SetPrinter(hPrinter, 2, @ppo, 0);
//定义打印机列表并选择 pFormName 纸张
end;
ClosePrinter(hPrinter);
end;