c#:winform 获取本地打印机列表(下拉列表实现)
效果
步骤
1、添加控件
①添加一个Label:作为标题控件,名为:Bigtitle
②添加一个ComboBox控件,名为:Printer_list
2、加载打印机列表
在窗体的构造函数或 Load
事件中加载打印机列表
public Form1()
{
InitializeComponent();//加载窗体内容
LoadPrinters(); // 加载打印机列表
}
写入方法
//引入打印机列表数据
private void LoadPrinters()
{
Printer_list.Items.Clear();//清除现有打印机列表
//遍历所有安装的打印机
foreach (string printer in PrinterSettings.InstalledPrinters)
{
//将每个打印机名称添加到 ComboBox 中
Printer_list.Items.Add(printer);
}
// 默认选择第一个打印机
if (Printer_list.Items.Count > 0)
{
Printer_list.SelectedIndex = 0;
}
}
完整代码
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
Form1.cs
using System;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Getprint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();//加载窗体内容
LoadPrinters(); // 加载打印机列表
}
private void Printer_list_SelectedIndexChanged(object sender, EventArgs e)
{
}
//引入打印机列表数据
private void LoadPrinters()
{
Printer_list.Items.Clear();//清除现有打印机列表
//遍历所有安装的打印机
foreach (string printer in PrinterSettings.InstalledPrinters)
{
//将每个打印机名称添加到 ComboBox 中
Printer_list.Items.Add(printer);
}
// 默认选择第一个打印机
if (Printer_list.Items.Count > 0)
{
Printer_list.SelectedIndex = 0;
}
}
private void Bigtitle_Click(object sender, EventArgs e)
{
}
private void Printer_list_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
Form1.Designer.cs
namespace Getprint
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Bigtitle = new System.Windows.Forms.Label();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.Printer_list = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// Bigtitle
//
this.Bigtitle.AutoSize = true;
this.Bigtitle.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Bigtitle.Location = new System.Drawing.Point(294, 22);
this.Bigtitle.Name = "Bigtitle";
this.Bigtitle.Size = new System.Drawing.Size(178, 20);
this.Bigtitle.TabIndex = 0;
this.Bigtitle.Text = "获取 打印机 列表";
this.Bigtitle.Click += new System.EventHandler(this.Bigtitle_Click);
//
// printDialog1
//
this.printDialog1.UseEXDialog = true;
//
// Printer_list
//
this.Printer_list.Font = new System.Drawing.Font("宋体", 12F);
this.Printer_list.FormattingEnabled = true;
this.Printer_list.Location = new System.Drawing.Point(201, 60);
this.Printer_list.Name = "Printer_list";
this.Printer_list.Size = new System.Drawing.Size(371, 28);
this.Printer_list.Sorted = true;
this.Printer_list.TabIndex = 2;
this.Printer_list.SelectedIndexChanged += new System.EventHandler(this.Printer_list_SelectedIndexChanged_1);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.Printer_list);
this.Controls.Add(this.Bigtitle);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label Bigtitle;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.ComboBox Printer_list;
}
}