C#设置默认打印机
项目中,需要选择打印机,切换打印机。demo如下(wpf应用程序):
Xaml:
<Window x:Class="PrintersApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<ComboBox x:Name="printerComboBox"/>
<Button Content="设置成打印机" Click="setDefaultPrintBtn"/>
</StackPanel>
</Grid>
</Window>
打印机LocalPrinter类:
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;
}
}
win api :
public class Externs
{
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机
}
逻辑代码:
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitprinterComboBox(); //初始化打印机下拉列表选项
} private void InitprinterComboBox()
{
List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表
foreach (String s in list)
{
printerComboBox.Items.Add(s); //将打印机名称添加到下拉框中
}
} private void setDefaultPrintBtn(object sender, RoutedEventArgs e)
{
if (printerComboBox.SelectedItem != null) //判断是否有选中值
{
if (Externs.SetDefaultPrinter(printerComboBox.SelectedItem.ToString())) //设置默认打印机
{
MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机成功!");
}
else
{
MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机失败!");
}
} }
}
运行截图:

下拉ComboBox,选择要设置为默认的打印机,点击“设置成默认打印机”按钮,设置成功:

C#设置默认打印机的更多相关文章
- windows设置默认打印机
实现这个功能需要使用windows api [DllImport("winspool.drv")] public static extern bool SetDefaultPrin ...
- Winform 判断打印机是否可用,实现设置默认打印机功能
Winform 判断打印机是否可用,实现设置默认打印机功能 http://www.cnblogs.com/zfanlong1314/p/3878563.html
- C#Winfrom系统打印机调用/设置默认打印机
实现如下效果: 实现方式如下: using System;using System.Drawing.Printing;using System.Runtime.InteropServices;usin ...
- VB6 获取和设置默认打印机
Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA&quo ...
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 利用ActiveX实现web页面设置本地默认打印机、纸张大小
通常web技术无法设置本地计算机的默认打印机,包括用代码设置纸张大小,如果业务系统中真遇到这种需求,只能通过其它辅助手段(比如ActiveX)实现.下面这段代码,出自网上被广泛使用的"泥人张 ...
- PC设置局域网打印机
打印机采用局域网网络连接方式,下面以Windows系统为例说明如何添加此打印机. 将电脑接入局域网 在“控制面板”中打开“设备与打印机”,点击“添加打印机” 在弹出列表中,会自动出现打印机型号,选中它 ...
- cad.net cad启动慢? cad2008启动慢? cad启动延迟? cad卡住? cad98%卡? 默认打印机!!
默认打印机是不是联网打印机,如果cad找不到这个打印机将会很慢才打开cad的界面(它真的不是卡死了,而是找不到...) 奇妙的是桌子至今都没有利用新建线程的方式来控制这个打印机等待,而是直接在主程序上 ...
- 如何以编程方式打印到在 MFC 中的非默认打印机
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105790245b09c0252bd7a74a2485d315d2390f0750 ...
随机推荐
- js--数组去重3种方法
js数组去重的三种常用方法总结 第一种是比较常规的方法 思路: 1.构建一个新的数组存放结果 2.for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组对比 3.若结果数组中没有该元素,则存 ...
- 百度JS模板引擎 baiduTemplate 1.0.6 版
A.baiduTemplate 简介 0.baiduTemplate希望创造一个用户觉得“简单好用”的JS模板引擎 注:等不及可以直接点左侧导航中的”C.使用举例“,demo即刻试用. 1.应用场景: ...
- const形参与非const形参
在程序设计中我们会经常调用函数,调用函数就会涉及参数的问题,那么在形参列表中const形参与非const形参对传递过来的实参有什么要求呢? 先来看一个简单的例子: #include <iostr ...
- 【转】js获取url传递参数
<Script language="javascript">var Request = new Object();Request = GetRequest();var ...
- asp.net分页代码(教你怎么实现)
直接上代码: css部分代码 .pageCss { float: right; width: auto; height: 30px; line-height: 30px; margin-right: ...
- bootcss
道友们,今天由贫道讲一下bootcss的编码规范: 首先黄金定律:一个项目一定要有一套编码规范,无伦项目有多少人参与都要一致. 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一 ...
- 解决Firefox访问12306"连接不受信任"的问题
用Firefox访问12306.cn, 总是提示"This Connection is Untrusted", 曾经有个"Add Exception" 按钮, ...
- HDU 1081 To The Max(动态规划)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- Excel教程(11) - 查找和引用函数
ADDRESS 用途:以文字形式返回对工作簿中某一单元格的引用. 语法: sheADDRESS(row_num,column_num,abs_num,a1,et_text) 参数:Row_num ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home
求1到N的最短路 注意有重边 跑一遍dijkstra就行 /* *********************************************** Author :Sun Yuefeng ...