人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

C#枚舉系統(tǒng)安裝的所有打印機(jī)

C#枚舉系統(tǒng)安裝的所有打印機(jī)

更新時間:2022-06-06 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

最近在論壇中不少網(wǎng)友問"如何把Windows安裝的所有打印機(jī)列出來",在下面的程序中我們將把系統(tǒng)中所安裝的打印機(jī)用列表框列出來,同時為默認(rèn)打印機(jī)設(shè)置缺省值。

  在下面的程序中我們用到了兩個主要的類,把所有的打印機(jī)列表出來用到了PrinterSettings 類,獲取系統(tǒng)默認(rèn)打印機(jī)用到了PrintDocument 類,下面我們就動手實(shí)踐一下吧。

  先新建一個windows form的工程,然后加入一個lable和一個comBox,就行啦,關(guān)鍵在下面啦,我們?nèi)绾潍@得默認(rèn)打印機(jī),就得用下面的語句。

PrintDocument prtdoc = new PrintDocument();
string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//獲取默認(rèn)的打印機(jī)名

  有了默認(rèn)的打印機(jī),我們再把所有的打印機(jī)列出來。

  PrinterSettings類有一個InstalledPrinters的屬性,不知是做什么的吧,查MSDN如下解釋:
PrinterSettings.InstalledPrinters 獲取安裝在計(jì)算機(jī)上所有打印機(jī)的名稱。

  在C#中如下定義:

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

  屬性值

  PrinterSettings.StringCollection,它表示安裝在計(jì)算機(jī)上所有打印機(jī)的名稱。

  異常

異常類型 條件
Win32Exception 未能枚舉可用的打印機(jī)

  備注

  可以使用已安裝的打印機(jī)名稱的集合向用戶提供要打印到的打印機(jī)選擇。

  下面的示例用已安裝的打印機(jī)填充 comboInstalledPrinters 組合框,并且還在選擇更改時使用 PrinterName 屬性設(shè)置用于打印的打印機(jī)。PopulateInstalledPrintersCombo 例程在窗體初始化時被調(diào)用。該示例假定存在名為 printDoc 的 PrintDocument 變量,并且存在特定的組合框。

[C#]
//下面括號內(nèi)的自己翻譯添加進(jìn)去的
private void PopulateInstalledPrintersCombo()
{
// Add list of installed printers found to the combo box.(將系統(tǒng)中所有的打機(jī)加入列表框)
// 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.(當(dāng)列表框改變時設(shè)置選擇的打印機(jī))

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

}

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

//程序說明:將系統(tǒng)中的所有打印機(jī)在列表框中列出
//程序變量: 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;
  ///
  /// 必需的設(shè)計(jì)器變量。
  ///
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗體設(shè)計(jì)器支持所必需的
   //
   InitializeComponent();
   PrintDocument prtdoc = new PrintDocument();
   string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//獲取默認(rèn)的打印機(jī)名
   foreach(String strPrinter in PrinterSettings.InstalledPrinters)
   //在列表框中列出所有的打印機(jī),
   { 
    printerList.Items.Add(strPrinter);
    if (strPrinter == strDefaultPrinter)//把默認(rèn)打印機(jī)設(shè)為缺省值
    {
     printerList.SelectedIndex = printerList.Items.IndexOf(strPrinter);
    }
   }
   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

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

  #region Windows Form Designer generated code
  ///
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  ///
  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 = "選擇打印機(jī):";
   //
   // 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 = "當(dāng)前系統(tǒng)未裝打印機(jī)";
   //
   // 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 = "打印機(jī)列表";
   this.ResumeLayout(false);

  }
  #endregion

  ///
  /// 應(yīng)用程序的主入口點(diǎn)。
  ///
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

 }
}

以上代碼在windows xp + vc.net 下測試通過,編譯后在Windows98上測

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 鄢陵县| 原阳县| 新巴尔虎右旗| 墨玉县| 和硕县| 宾川县| 衡东县| 舒城县| 吉首市| 沿河| 宣威市| 偏关县| 辽宁省| 清流县| 陆川县| 福鼎市| 福建省| 汉源县| 霞浦县| 黄冈市| 凌云县| 疏附县| 宝鸡市| 龙川县| 景德镇市| 历史| 长寿区| 图们市| 吉水县| 温宿县| 崇明县| 淮阳县| 蒙自县| 昭通市| 务川| 贺州市| 洪湖市| 关岭| 恩施市| 漯河市| 茂名市|