C# 默认打印机设置

 

private void frmBarCode_Load(object sender, EventArgs e)
{

 bindCbox();

}

#region 选择打印机
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机

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;
}

//获取当前默认打印机信息

private void bindCbox()
{
List<String> PrinterList = new List<String>();
PrinterList = GetLocalPrinters();
foreach (String strpinter in PrinterList)
{
cmbPrinter.Properties.Items.Add(strpinter);
}
if (cmbPrinter.SelectedItem == null)
{
cmbPrinter.SelectedIndex = 0;
}
else
{
cmbPrinter.SelectedIndex = 0;
}
}

private void cmbPrinter_SelectedIndexChanged(object sender, EventArgs e)
{
SetDefaultPrinter(cmbPrinter.SelectedText);
}
#endregion

posted @ 2019-07-23 16:24  limitpjz  阅读(954)  评论(0编辑  收藏  举报