要獲得在系統中定義的所有打印機表單列表:
uses
winspool, printers;
...
procedure TForm1.Button1Click(Sender: TObject);
var
HPrinter: THandle;
Forms: array of TFormInfo1;
Count, Needed, Returned: DWORD;
i: Integer;
begin
Memo1.Clear;
if OpenPrinter(nil, HPrinter, nil) then begin
try
if not EnumForms(HPrinter, 1, nil, 0, Needed, Returned) then begin
// we should fail here since we didn't pass a buffer
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
RaiseLastOSError;
Count := (Needed div SizeOf(TFormInfo1)) + 1;
SetLength(Forms, Count);
if EnumForms(HPrinter, 1, @Forms[0], SizeOf(TFormInfo1) * Count, Needed,
Returned) then begin
if Returned < Count then
SetLength(Forms, Returned);
for i := 0 to Returned - 1 do begin
Memo1.Lines.Add(Format('Paper name: %s, Paper size: %dmm x %dmm',
[Forms[i].pName,
Forms[i].Size.cx div 1000,
Forms[i].Size.cy div 1000]))
end;
end else
RaiseLastOSError;
end;
finally
ClosePrinter(HPrinter);
end;
end else
RaiseLastOSError;
end;
請準確定義「所有紙張大小」 –