C#設置默認打印機


開發中經常會遇到需要用到打印機的問題,那么我們現在來一個Demo修改系統默認打印機。先看運行效果吧。(主要為了展示代碼和功能,界面就隨便拖拉了一個,比較丑,不要介意。)

界面構建非常簡單,首先新建一個Form窗體,拉一個comboBox控件和一個Button然后就可以了。

接下來我們看下代碼。

首先是加載本地打印機的類LocalPrinter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
   public class LocalPrinter
    {
        private static PrintDocument fPrintDocument = new PrintDocument();
        //獲取本機默認打印機名稱
        public static String DefaultPrinter()
        {
            return fPrintDocument.PrinterSettings.PrinterName;
        }
        public static List<String> GetLocalPrinters()
        {
            List<String> fPrinters = new List<String>();
            fPrinters.Add(DefaultPrinter()); //默認打印機始終出現在列表的第一項
            foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
            {
                if (!fPrinters.Contains(fPrinterName))
                {
                    fPrinters.Add(fPrinterName);
                }
            }
            return fPrinters;
        }
    }
}

然后是調用windows  打印機操作api的類Externs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; //必須引入這個命名空間

namespace WindowsFormsApplication1
{
    public class Externs
    {
        [DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(String Name); //調用win api將指定名稱的打印機設置為默認打印機
    }
}

然后就是窗體的Form的代碼了,其實就是一個按鈕點擊事件和初始化的操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitprinterComboBox(); //加載數據
        }

        /// <summary>
        /// 初始化ComboBox數據
        /// </summary>
        private void InitprinterComboBox()
        {
            List<String> list = LocalPrinter.GetLocalPrinters(); //獲得系統中的打印機列表
            foreach (String s in list)
            {
                printerComboBox.Items.Add(s); //將打印機名稱添加到下拉框中
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (printerComboBox.SelectedItem != null) //判斷是否有選中值
            {
                if (Externs.SetDefaultPrinter(printerComboBox.SelectedItem.ToString())) //設置默認打印機
                {
                    MessageBox.Show(printerComboBox.SelectedItem.ToString() + "設置為默認打印機成功!");
                }
                else
                {
                    MessageBox.Show(printerComboBox.SelectedItem.ToString() + "設置為默認打印機失敗!");
                }
            }
        }
    }
}

這樣,運行就可以設置默認打印機了。

那么,如何測試是否成功呢?很簡單,打開控制面板,在打開打印機的界面,然后運行程序,設置默認打印機,看看是否會改變即可。筆者測試的結果是可以得,但是如果電腦本身沒有設置默認打印機,那么設置也會成功,但是那個綠色的默認鈎沒有顯示出來。這時候可以手動設置一下默認,然后再 通過軟件修改就會顯示那個綠色的默認鈎了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM