C#枚舉系統安裝的所有印表機

來源:互聯網
上載者:User
在下面的程式中我們將把系統中所安裝的印表機用列表框列出來,同時為預設印表機設定預設值。

  在下面的程式中我們用到了兩個主要的類,把所有的印表機列表出來用到了PrinterSettings 類,擷取系統預設印表機用到了PrintDocument 類,下面我們就動手實踐一下吧。

  先建立一個windows form的工程,然後加入一個lable和一個comBox,就行啦,關鍵在下面啦,我們如何獲得預設印表機,就得用下面的語句。

PrintDocument prtdoc = new PrintDocument();
string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//擷取預設的印表機名

  有了預設的印表機,我們再把所有的印表機列出來。

  PrinterSettings類有一個InstalledPrinters的屬性,不知是做什麼的吧,查MSDN如下解釋:
PrinterSettings.InstalledPrinters 擷取安裝在電腦上所有印表機的名稱。

  在C#中如下定義:

[C#]
[Serializable]
[ComVisible(false)]
public static PrinterSettings.StringCollection InstalledPrinters
{get;}

  屬性值

  PrinterSettings.StringCollection,它表示安裝在電腦上所有印表機的名稱。

  異常

異常類型 條件
Win32Exception 未能枚舉可用的印表機

  備忘

  可以使用已安裝的印表機名稱的集合向使用者提供要列印到的印表機選擇。

  下面的樣本用已安裝的印表機填充 comboInstalledPrinters 組合框,並且還在選擇更改時使用 PrinterName 屬性設定用於列印的印表機。PopulateInstalledPrintersCombo 常式在表單初始化時被調用。該樣本假定存在名為 printDoc 的 PrintDocument 變數,並且存在特定的組合框。

[C#]
//下面括弧內的自己翻譯添加進去的
private void PopulateInstalledPrintersCombo()
{
// Add list of installed printers found to the combo box.(將系統中所有的打機加入列表框)
// The pkInstalledPrinters string will be used to provide the display string.(列表框中顯示的字串由pkInstalledPrinters提供)
foreach(String pkInstalledPrinters in
PrinterSettings.InstalledPrinters)
{
comboInstalledPrinters.Items.Add(pkInstalledPrinters);
}
}

private void comboInstalledPrinters_SelectionChanged(object sender, System.EventArgs e)
{

// Set the printer to a printer in the combo box when the selection changes.(當列表框改變時設定選擇的印表機)

if (comboInstalledPrinters.SelectedIndex != -1)
{
// The combo box's Text property returns the selected item's text, which is the printer name.(將選擇的印表機名在列表框中顯示)
printDoc.PrinterSettings.PrinterName= comboInstalledPrinters.Text;
}

}

  看了MSDN的說明,懂多了吧,下面是我寫練習完整代碼.

//程式說明:將系統中的所有印表機在列表框中列出
//程式變數: PrintDocument prtdoc、string strDefaultPrinter
//編寫人:蠶蛹(sillnet@163.net)
//日期:2003-03-20
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PrinterList
{
 ///
 /// Form1 的摘要說明。
 ///
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.ComboBox printerList;
  ///
  /// 必需的設計器變數。
  ///
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 表單設計器支援所必需的
   //
   InitializeComponent();
   PrintDocument prtdoc = new PrintDocument();
   string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//擷取預設的印表機名
   foreach(String strPrinter in PrinterSettings.InstalledPrinters)
   //在列表框中列出所有的印表機,
   { 
    printerList.Items.Add(strPrinter);
    if (strPrinter == strDefaultPrinter)//把預設印表機設為預設值
    {
     printerList.SelectedIndex = printerList.Items.IndexOf(strPrinter);
    }
   }
   //
   // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
   //
  }

  ///
  /// 清理所有正在使用的資源。
  ///
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  ///
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  ///
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.printerList = new System.Windows.Forms.ComboBox();
   this.SuspendLayout();
  //
  // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 24);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(72, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "選擇印表機:";
   //
   // printerList
   //
   this.printerList.Location = new System.Drawing.Point(88, 22);
   this.printerList.Name = "printerList";
   this.printerList.Size = new System.Drawing.Size(192, 21);
   this.printerList.TabIndex = 1;
   this.printerList.Text = "當前系統未裝印表機";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(288, 61);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.printerList,
      this.label1});
   this.Name = "Form1";
   this.Text = "印表機列表";
   this.ResumeLayout(false);

  }
  #endregion

  ///
  /// 應用程式的主進入點。
  ///
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.