实现如下效果:

实现方式如下:

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

}
}

 

最新文章

  1. mysql autocommit=OFF导致wordpress 建立数据库连接时出错
  2. MongoBD解决没有自动增长ID 的问题
  3. java面试每日一题13
  4. android上传文件到wamp服务器
  5. hbase(ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet)
  6. c++中的指针问题
  7. 修改一行SQL代码 性能提升了N倍
  8. Mysql RR隔离更新列没有索引 会锁全表
  9. 《HBase权威指南》读书笔记----简介
  10. struts2语法--error页面如何捕获?
  11. 手把手教你用Mysql-Cluster-7.5搭建数据库集群
  12. makefile介绍1.0
  13. GC机制总结
  14. AngularJS实现的自定义过滤器简单示例
  15. Java中方法定义和调用的学习
  16. vue三级联动
  17. Java第一次实训课的作业
  18. window.opener和window.open
  19. mysql学习笔记--数据库多表查询
  20. hdfs 例子

热门文章

  1. (实用篇)PHP ftp上传文件操作类
  2. [转载] TCP与UDP对比
  3. CMOS、BIOS
  4. 常用tcode
  5. Html与CSS布局技巧
  6. SpringSecurity相关配置【SpringSecurityConfig】
  7. Codeforces Round #135 (Div. 2)
  8. C/C++程序员必须熟练应用的开源项目(转-清风小阁)
  9. php文件遍历类:FileBianli.class.php
  10. Java基础了解