zoukankan      html  css  js  c++  java
  • 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

    }
    }

     

  • 相关阅读:
    阮一峰的网络日志
    解决Windows路径太长的目录以及文件名超长删除的问题
    LVM扩容根分区
    Centos7 搭建prometheus+Grafana监控
    python3 读取txt文件数据,绘制趋势图,matplotlib模块
    linux python3安装whl包时报错解决:is not a supported wheel on this platform
    堆排序
    nginx: [emerg] "upstream" directive is not allowed here in .../.../.../*.conf
    Github私有仓库使用设置
    AWK处理重复Hosts
  • 原文地址:https://www.cnblogs.com/YYkun/p/5662902.html
Copyright © 2011-2022 走看看