C#调用系统打印机(XP-58小票打印)

此代码示例展示了如何在C#中使用系统打印机,特别是XP-58小票打印机。首先检查打印机是否为默认的XP-58,然后获取打印机状态,如果状态正常则开始打印。打印内容包括时间、回收类型和重量等信息。程序通过`PrintDocument`类实现了打印功能,并处理了打印页面的事件。
摘要由CSDN通过智能技术生成

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);
                
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值