简体   繁体   English

如何找到是否安装了打印机(使用Delphi)

[英]How can I find if there are printers installed (using Delphi)

My program is written in Delphi (object oriented pascal). 我的程序是用Delphi编写的(面向对象的pascal)。 When I try to print anything I get the message "There is no default printer currently selected." 当我尝试打印任何内容时,我收到消息“当前没有选择默认打印机”。 on computers with no printers installed. 在未安装打印机的计算机上。

To prevent this I want to check if there are any printers installed. 为了防止这种情况,我想检查是否安装了任何打印机。 Is there any way to check if any printers are installed? 有没有办法检查是否安装了任何打印机?

use Printers;

Printer.Printers.Count gives you the number of printers installed Printer.Printers.Count为您提供安装的打印机数量

The comments form the Printers unit in D2007: 这些评论来自D2007中的打印机单元:

  { TPrinter }

  { The printer object encapsulates the printer interface of Windows.  A print
    job is started whenever any redering is done either through a Text variable
    or the printers canvas.  This job will stay open until EndDoc is called or
    the Text variable is closed.  The title displayed in the Print Manager (and
    on network header pages) is determined by the Title property.

    EndDoc - Terminates the print job (and closes the currently open Text).
      The print job will being printing on the printer after a call to EndDoc.
    NewPage - Starts a new page and increments the PageNumber property.  The
      pen position of the Canvas is put back at (0, 0).
    Canvas - Represents the surface of the currently printing page.  Note that
      some printer do not support drawing pictures and the Draw, StretchDraw,
      and CopyRect methods might fail.
    Fonts - The list of fonts supported by the printer.  Note that TrueType
      fonts appear in this list even if the font is not supported natively on
      the printer since GDI can render them accurately for the printer.
    PageHeight - The height, in pixels, of the page.
    PageWidth - The width, in pixels, of the page.
    PageNumber - The current page number being printed.  This is incremented
      when ever the NewPage method is called.  (Note: This property can also be
      incremented when a Text variable is written, a CR is encounted on the
      last line of the page).
    PrinterIndex - Specifies which printer in the TPrinters list that is
      currently selected for printing.  Setting this property to -1 will cause
      the default printer to be selected.  If this value is changed EndDoc is
      called automatically.
    Printers - A list of the printers installed in Windows.
    Title - The title used by Windows in the Print Manager and for network
      title pages. }

Here is a code snippet that populates a image combo box with all the printers currently installed: 这是一个代码片段,用于填充当前安装的所有打印机的图像组合框:

procedure TMyForm.RefreshPrinterList;
var
  I: Integer;
  NewItem: TComboExItem;
  PPrinterEnumArray, PLocator: PPrinterInfo2;
  ArraySize, BufferSize: cardinal;
  strBuffer: string;
const
  idx_Default_Net_printer = 0;
  idx_Net_printer = 1;
  idx_Default_Local_printer = 2;
  idx_Local_printer = 3;

begin
  DefaultPrinterName := getDefaultPrinterName;
  cbPrinterList.ItemsEx.Clear;
  // S.G. 4/4/2008: list all other printers
  // S.G. 4/4/2008: Get the necessary buffer size
  ArraySize := 0;
  BufferSize := 0;
  PPrinterEnumArray := nil;
  EnumPrinters(PRINTER_ENUM_LOCAL or PRINTER_ENUM_CONNECTIONS, nil, 2, nil, 0, BufferSize, ArraySize);
  PPrinterEnumArray := AllocMem(BufferSize);
  try
    if EnumPrinters(PRINTER_ENUM_LOCAL or PRINTER_ENUM_CONNECTIONS, nil, 2, PPrinterEnumArray, BufferSize, BufferSize, ArraySize) then
    begin
      PLocator := PPrinterEnumArray;
      if ArraySize > 0 then
      begin
        for I := 0 to ArraySize - 1 do    // Iterate
        begin
          NewItem := cbPrinterList.ItemsEx.Add;
          strBuffer := StrPas(PLocator^.pPrinterName);
          UniqueString(strBuffer); // make sure we have a unique string instance and not a pointer
          NewItem.Caption := strBuffer;

          if AnsiSameText(DefaultPrinterName, strBuffer) then
          begin
            // default printer
            if  (PRINTER_ATTRIBUTE_LOCAL AND PLocator^.Attributes) <> 0 then
            begin
              // Local, default printer
              NewItem.ImageIndex := idx_Default_Local_printer;
            end
            else
            begin
              // Network default printer
              NewItem.ImageIndex := idx_Default_Net_printer;
            end;
            cbPrinterList.ItemIndex := NewItem.Index;
          end
          else
          begin
            // default printer
            if  (PRINTER_ATTRIBUTE_LOCAL AND PLocator^.Attributes) <> 0 then
            begin
              // Local, default printer
              NewItem.ImageIndex := idx_Local_printer;
            end
            else
            begin
              // Network default printer
              NewItem.ImageIndex := idx_Net_printer;
            end;
          end;
          Inc(PLocator);
        end;    // for
      end;
    end;
  finally // wrap up
    FreeMem(PPrinterEnumArray);
  end;    // try/finally
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Delphi中的打印机单元 - Using the Printers Unit in Delphi 如何在Delphi中使用“文件查找”执行布尔“ AND”搜索? - How can I perform a boolean 'AND' search in Delphi using 'Find in Files? 在Delphi中:如何在不使用参数的情况下找到递归深度? - In Delphi: How can I find the recursion depth without using a parameter? 如何在IDE中获取所有已安装的组件? (DELPHI) - How can I get all installed components inside IDE? (Delphi) 如何查找在delphi中是否安装了firebird以及在何处安装了firebird - How to find if and where is installed firebird in delphi 在哪里可以找到将DUnit与Delphi 2007或更新版本一起使用的介绍? - Where can I find an introduction to using DUnit with Delphi 2007 or newer? 如何在Delphi中查找DataSet是否为主/详细关系中的主服务器? - How can I find if a DataSet is the master in a master/detail relationship in Delphi? 如何在Delphi应用程序中找到所有单元? - How can I find all the units in my Delphi app? Delphi如何从resourcestring单元中找到资源名称 - Delphi How can i find a resource name from resourcestring unit 如何将查找对话框中的文本框(编辑)更改为 Delphi 中的组合框? - How can I change the textbox (Edit) in a Find dialog to a Combobox in Delphi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM