C#调用系统打印机(XP-58小票打印) 需要安装XP-58驱动。
// An highlighted block using System; using System.Drawing; using System.Drawing.Printing; using System.Globalization; using System.Management; using System.Windows.Forms; namespace 实验 { public class USBPrinter { private string defaultPrinter; //private string streamtext; private PrintDocument docToPrint = new PrintDocument();//创建一个PrintDocument的实例 private string carCardNum; private string trashTypeName; private string trashWeight; private string driverUserName; private string loginUserName; public USBPrinter() { // // TODO: 在此处添加构造函数逻辑 // //this.docToPrint.DefaultPageSettings.PaperSize = new PaperSize("Custum", default, 600); this.docToPrint.PrintPage += new PrintPageEventHandler(DocToPrint_PrintPage); }//将事件处理函数添加到PrintDocument的PrintPage中 public bool IsDefaultPrinter() { using (PrintDocument pd = new PrintDocument()) { string sDefault = pd.PrinterSettings.PrinterName;//默认打印机名 //MessageBox.Show(sDefault); if (sDefault == "XP-58") { defaultPrinter = sDefault; return true; } else { defaultPrinter = "默认打印机不是XP-58"; return false; } } } public enum PrinterStatus { 其他状态 = 1, //比如 暂停状态 未知, 空闲, //就绪状态 正在打印, 预热, 停止打印, 打印中, 离线 //脱机 状态 } public PrinterStatus GetPrinterStat(string PrinterDevice) { PrinterStatus ret = 0; string path = @"win32_printer.DeviceId='" + PrinterDevice + "'"; ManagementObject printer = new ManagementObject(path); printer.Get(); if ((bool)printer.Properties["WorkOffline"].Value) { return PrinterStatus.离线; //MessageBox.Show("离线"); } else { ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value); //MessageBox.Show(Convert.ToString(printer.Properties["PrinterStatus"].Value)); return ret; } } public void StartPrint(string carCardNum, string trashTypeName, string trashWeight, string driverUserName, string loginUserName) { //this.streamtext = text; this.carCardNum = carCardNum; this.trashTypeName = trashTypeName; this.trashWeight = trashWeight; this.driverUserName = driverUserName; this.loginUserName = loginUserName; if (IsDefaultPrinter()) { docToPrint.Print(); } else { MessageBox.Show(defaultPrinter); } Release(); } private void DocToPrint_PrintPage(object sender, PrintPageEventArgs e)//设置打印机开始打印的事件处理函数 e.MarginBounds.Y { e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //像素偏移方式,像素在水平和垂直距离上均偏移若干个单位,以进行高速锯齿消除。 e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; //也可以通过设置Graphics对不平平滑处理方式解决,代码如下: //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Font printFont = new Font("Arial", (float)9.3, FontStyle.Regular); //StringFormat stringFormat = new StringFormat(); // Draw the content. //string text = streamtext; //e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 0, 0); int marginTop = 10; int dis = 25; e.Graphics.DrawString(" ", printFont, Brushes.Black, 0, 0); e.Graphics.DrawString("回收凭条", new Font("Arial", 10, FontStyle.Bold), Brushes.Black, 63, marginTop + dis * 2); e.Graphics.DrawString("时 间: " + DateTime.Now.ToString("G", CultureInfo.CreateSpecificCulture("zh-CN")), printFont, Brushes.Black, 0, marginTop + dis * 4); e.Graphics.DrawString("回收类型: " + trashTypeName, printFont, Brushes.Black, 0, marginTop + dis * 5); e.Graphics.DrawString("回收净重: " + trashWeight + "kg", printFont, Brushes.Black, 0, marginTop + dis * 6); e.Graphics.DrawString(".", printFont, Brushes.Gray, 0, dis * 11 + 15); } private void Release() { docToPrint.Dispose(); trashTypeName = null; trashWeight = null; carCardNum = null; driverUserName = null; loginUserName = null; } } }在form窗口调用
USBPrinter printer = new USBPrinter(); printer.IsDefaultPrinter(); //MessageBox.Show(printer.GetPrinterStat("XP-58").ToString()); try { //if (printer.GetPrinterStat("XP-58").ToString() == "离线") //{ // MessageBox.Show("打印机XP-58未连接"); // return; //} //else if (printer.GetPrinterStat("XP-58").ToString() == "其他状态" || printer.GetPrinterStat("XP-58").ToString() == "未知") //{ // MessageBox.Show("打印机XP-58连接出错"); // return; //} printer.StartPrint("111", "111", "111", "111", "111"); } catch (Exception ex) { MessageBox.Show(ex.Message); }