C#Winfrom系统打印机调用/设置默认打印机
实现如下效果:
实现方式如下:
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace PISS.View.CustomControl
{
public partial class PrinterConfigMessBox : Form
{
#region 定义、构造、初始化
[DllImport("winspool.drv")]
//调用win api将指定名称的打印机设置为默认打印机
public static extern bool SetDefaultPrinter(String Name);
private static PrintDocument PrintDocument = new PrintDocument();
public string PrinterName { get; set; }
//获取本机默认打印机名称
public static String DefaultPrinter()
{
return PrintDocument.PrinterSettings.PrinterName;
}
public PrinterConfigMessBox(string message)
{
InitializeComponent();
this.lblMessage.Text = message;
}
private void PrinterConfigMessBox_Load(object sender, EventArgs e)
{
foreach (var item in PrinterSettings.InstalledPrinters)
{
this.tvList.Nodes.Add(item.ToString());
}
PrinterName = PrintDocument.PrinterSettings.PrinterName;
SetDefaultSelectNode();
}
/// <summary>
/// 设置默认选中项
/// </summary>
private void SetDefaultSelectNode()
{
foreach (TreeNode item in this.tvList.Nodes)
{
if (!item.Text.Equals(PrinterName)) continue;
this.tvList.SelectedNode = item;
break;
}
}
#endregion
#region event
private void btnPrint_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void tvList_AfterSelect(object sender, TreeViewEventArgs e)
{
PrinterName = this.tvList.SelectedNode.Text;
PrinterConfigMessBox.SetDefaultPrinter(PrinterName);
SetDefaultSelectNode();
}
#endregion
}
}
最新文章
- mysql autocommit=OFF导致wordpress 建立数据库连接时出错
- MongoBD解决没有自动增长ID 的问题
- java面试每日一题13
- android上传文件到wamp服务器
- hbase(ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet)
- c++中的指针问题
- 修改一行SQL代码 性能提升了N倍
- Mysql RR隔离更新列没有索引 会锁全表
- 《HBase权威指南》读书笔记----简介
- struts2语法--error页面如何捕获?
- 手把手教你用Mysql-Cluster-7.5搭建数据库集群
- makefile介绍1.0
- GC机制总结
- AngularJS实现的自定义过滤器简单示例
- Java中方法定义和调用的学习
- vue三级联动
- Java第一次实训课的作业
- window.opener和window.open
- mysql学习笔记--数据库多表查询
- hdfs 例子