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

    }
    }

     

  • 相关阅读:
    0909 谈谈我对操作系统的理解
    实验四 主存空间的分配和回收模拟
    12.03进程调度实验点评
    实验三进程调度实验
    实验二 作业调度模拟程序编写
    实验一 DOS命令解释程序的编写
    0909 随笔第一季
    实验四 主存空间的分配和回收模拟
    实验三 进程调度模拟实验
    实验二 作业调度模拟实验
  • 原文地址:https://www.cnblogs.com/YYkun/p/5662902.html
Copyright © 2011-2022 走看看