首页 > 解决方案 > 如何调用打印机的驱动程序设置对话框

问题描述

我正在构建自己的对话框,而不是使用标准的 PrintDialog。
我希望能够调用打印机驱动程序自己的设置对话框,即好像有人单击了 PrintDialog 中的属性按钮。
你能建议一种方法吗?

标签: delphi

解决方案


我以前没有使用过这个 API,但在我看来你可以使用这个DocumentProperties函数。

一个最小的例子(使用默认打印机):

var
  PrinterName: string;
  BufLen: Cardinal;
  PrinterHandle: THandle;
begin

  GetDefaultPrinter(nil, @BufLen);
  SetLength(PrinterName, BufLen);
  GetDefaultPrinter(PChar(PrinterName), @BufLen);
  SetLength(PrinterName, BufLen - 1);

  if not OpenPrinter(PChar(PrinterName), PrinterHandle, nil) then
  begin
    ShowMessage('Could not open printer.');
    Exit;
  end;

  try
    DocumentProperties(Handle, PrinterHandle, PChar(PrinterName), nil, nil, DM_IN_PROMPT)
    // possibly do other things that might raise an exception
  finally
    ClosePrinter(PrinterHandle);
  end;

如果您还添加了相应的标志,则可以将nil指针替换为DEVMODE包含初始设置和用户在 GUI 中选择的设置的结构。有关详细信息,请参阅文档。


推荐阅读