using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace Printers
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
InitprinterComboBox(); //初始化打印机下拉列表选项
}
private void InitprinterComboBox()
{
List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表
foreach (String s in list)
{
printerComboBox.Items.Add(s); //将打印机名称添加到下拉框中
}
} private void printButton_Click(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() + "设置为默认打印机失败!");
}
}
}
}
}

LocalPrinter.cs

按 Ctrl+C 复制代码

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

namespace Printers
{
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;
}
}
}

按 Ctrl+C 复制代码

Externs.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; namespace Printers
{
class Externs
{
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机
}
}

最新文章

  1. 2.3属性在 ASP.NET Web API 2 路由
  2. 世界国家 的数据库sql
  3. 【译】Java中的枚举
  4. 如何用ZBrush做人体造型雕刻
  5. java.lang.UnsupportedOperationException: Not supported by BasicDataSource
  6. android xml解析添加到listview中的问题
  7. 【filter】springmvc web.xml
  8. 【DataStructure】Description and usage of queue
  9. hdu 4714 Tree2cycle dp
  10. bootstrap中下拉菜单点击事件 uncaught syntaxerror unexpected end of input异常问题
  11. mysql-innoDB-多版本并发控制(MVCC)
  12. iOS-image图片压缩
  13. 不解,排名靠前那么多的人为什么抄袭我的activit博文??
  14. 记录一些移动端H5,小程序视觉还原问题及方法
  15. Python内置函数(8)——bytes
  16. Oracle 11g R2 for Win10(64位)的安装步骤
  17. python删除列表元素
  18. bootstrap modal 移除数据
  19. Pycharm初始创建项目和环境搭建
  20. Code Review —— by12061154Joy

热门文章

  1. 图片延迟加载库Layzr
  2. [译]GC专家系列2:Java 垃圾回收的监控
  3. 增加p()函数,方便开发中对变量打印调试
  4. 手把手教你写LKM rookit! 之 杀不死的pid&amp;root后门
  5. Android SDK离线安装方法详解(加速安装) 转
  6. 【原创】一起学C++ 之指针、数组、指针算术 ---------C++ primer plus(第6版)
  7. [Learn Android Studio 汉化教程]第三章:使用 Android Studio 编程
  8. shell调用sqlplus批量执行sql文件
  9. 使用NPIO操作Excel
  10. oracle 表空间、用户名 相关语句