在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → Zebar打印机 示例源码

Zebar打印机 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:1.04M
  • 下载次数:153
  • 浏览次数:560
  • 发布时间:2020-11-12
  • 实例类别:C#语言基础
  • 发 布 人:grist
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 实现 程序 C#

实例介绍

【实例简介】实现斑马打印机的控制格式

【实例截图】简单的生成ZPL指令并保存为模板的小demo

from clipboard

【核心代码】

  //使用斑马打印机打印
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                RawPrinterHelper.SendStringToPrinter(comboBox1.Text, this.textBox1.Text);

                ////实例化一个打印类
                ZebraPrinter printer = new ZebraPrinter();
                FileStream fs = new FileStream("Template.TXT", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);

                while (sr.Peek() > 0)
                {
                    string str = sr.ReadLine();
                    if (!string.IsNullOrEmpty(str))
                    {
                        ///////定义两个标签值
                        //string str1 = tag1.Text;
                        //string str2 = tag2.Text;

                        ////依照格式,格式化成新的字符串
                        //string str = string.Concat("条码1", "^", "条码2", "~", str1, "^", str2);   //双排条形码
                        str = string.Concat(" ", "~", str);      //单排条形码    ,Concat连接三个指定实例
                        //str = string.Concat(" ", "^", " ", "~", str, "^", str);   //双排条形码
                        label6.Text = str;
                        printer.PrintLabEx("Template.TXT", str, comboBox1.Text);//printPath:模板名程,从文本框里选。str为ZPL命令。  comboBox1.Text:打印机名称
                    }
                }
                fs.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" ex.Message);
            }
        }

        //扫描打印机
        private void Form1_Load(object sender, EventArgs e)
        {
            //显示时间
            timer1.Start();
            //选择打印机
            if (string.IsNullOrEmpty(comboBox1.Text))
            {
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
            }

            string template;
            //加载所有模板
            string[] files = Directory.GetFiles("Template");//("D:\\Visual Studio 2019\\VS Project\\Zebra\\ZebarTest1\\Template");
            foreach (string item in files)
            {
                template = item.ToString();
                string str1 = template.Remove(0, 9);
                //string str1 = template.SubString(8);
                comboBox3.Items.Add(str1);
            }

            ////获取本地计算机上安装的全部打印机,保存到集合strCollects中
            //PrinterSettings.StringCollection strCollects = PrinterSettings.InstalledPrinters;
            //string strPrinterName = string.Empty;
            ////遍历集合,将全部打印机载入到下拉列表comboBox1中
            //foreach (String strName in strCollects)
            //{
            //    strPrinterName = strName.ToString();
            //    comboBox1.Items.Add(strPrinterName);
            //}
            //comboBox1.SelectedIndex = 0x00;
            #region 绑定打印机名称
            foreach (string item in PrinterSettings.InstalledPrinters)//获取安装在本计算机上的所有打印机名称
            {
                comboBox1.Items.Add(item);
            }
            if (comboBox1.Items.Contains("ZDesigner GK888t (EPL)"))
            {
                comboBox1.Text = "ZDesigner GK888t (EPL)";
            }

            //备用
            //private void Smt_Zebra_WinForm_ZPL_Load(object sender, EventArgs e)
            //{
            //    PrinterSettings.StringCollection _strCollects = PrinterSettings.InstalledPrinters;

            //    string _strPrinterName = string.Empty;
            //    foreach (String _strName in _strCollects)
            //    {
            //        _strPrinterName = _strName.ToString();
            //        comboBox1.Items.Add(_strPrinterName);
            //    }
            //    comboBox1.SelectedIndex = 0x00;
            //}

            #endregion
            #region 设置默认值
            cbFenBianLv.SelectedIndex = 0;
            txtBiaoQianX.Text = "0";
            txtBiaoQianY.Text = "0";
            txtPrintX.Text = "230";
            txtPrintY.Text = "230";
            cbZhaiTiaoKuanDu.Text = "1";
            cbBiLi.SelectedIndex = 0;
            txtBarHeight.SelectedIndex = 0;
            cbPSD.SelectedIndex = 0;
            cbHSD.SelectedIndex = 3;
            cbFSD.SelectedIndex = 3;
            cbPrintZhuShi.SelectedIndex = 0;
            cbZhuShiHangWeiZhi.SelectedIndex = 1;
            cbZitiName.SelectedIndex = 0;
            cbZitiFangXiang.SelectedIndex = 0;
            txtH.SelectedIndex = 0;
            txtW.SelectedIndex = 0;
            txtPrintNeiRong.Text = "test text";

            //二维码参数
            cbQrOrientation.SelectedIndex = 0;
            cbQrModel.SelectedIndex = 1;
            cbQrMagnification.SelectedIndex = 1;
            cbQrSafe.SelectedIndex = 0;
            cbQrMaskValue.SelectedIndex = 0;
            #endregion
        }

        //激活配方
        public string printPath;
        private void button3_Click(object sender, EventArgs e)
        {
            printPath = comboBox3.Text;
            if (printPath == String.Empty)
            {
                MessageBox.Show("请选择你的模板");
                comboBox3.Focus();
            }
            else
            {
                //MessageBox.Show(printPath);
                //【1】创建文件流
                FileStream fs = new FileStream("Template\\" printPath, FileMode.Open);
                //【2】创建读取器
                StreamReader sr = new StreamReader(fs);
                //【3】以流的方式读取数据
                this.textBox1.Text = sr.ReadToEnd();
                //【4】关闭读取器
                sr.Close();
                //【5】关闭文件流
                fs.Close();
                //  RawPrinterHelper.SendStringToPrinter(comboBox1.Text, this.textBox1.Text);
            }
        }

        //参数设置打印
        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                string printNeiRong = "^FD" txtPrintNeiRong.Text "^FS";       //打印内容
                textBox3.Text = "Print==>" txtPrintNeiRong.Text;
                #region 指令语法
                /*                带【】符号表示可以不设置
                 ^XA              表示新标签内容开始  
                 【^JMn 】        改变每毫米打点数 n=A 打印机使用当前最高分辨率,n=B 打印分辨率减半
                 ^LH0,0           设置标签原点座标 x:标签原点的横坐标; y:标签原点的纵坐标 
                 ^FO203,203       设置相对于标签原点的打印起始点座标
                 ^BY3,2.4,50      设置条码格式参数   
                                  w:条码中窄条的宽度,单位 dot,默认值:2dot,值范围:1~10dot; 
                                  r:窄条和宽条的比例,默认值:3,值范围:2~3; 
                                  h:条码高度:默认值 10dot 值范围 100~标签高度

                 //^B3N,Y,,Y,N
                 【^PRp,s,b 】    改变打印速度 
                                  p:打印速度,默认值:A,值范围:A~E(或2~8); 
                                  s:回转速度,默认值:D,值范围:A~E(或2~8)A~E;  
                                  b:反馈速度,默认值:D,值范围:A~E(或2~8)A~E;
                 
                 【^Af,o,h,w】    设置字符字体 
                                  f:字体参数; 
                                  o:字符方向参数,默认值:N,值范围:N(正常)、R(90度)、I(180)、B(270度); 
                                  h:字符高度; 
                                  w:字符宽度;
                 * 
                 * 
                【^BCo,h,f,g,e,m】          设置128-B码打印格式 
                                            o:字符方向参数,默认值:N,值范围:N、R、I、B;     
                                            h:条码高度; 
                                            f:打印解释行,Y为打印,N为不打印; 
                                            g、解释行在条码上方还是下方,Y在条码上方,N在条码下方


                 ^FDABC123^FS     将FD后面的字符打印出来 即打印内容    (^FS表明要打印的内容结束)
                 ^XZ 
                 */
                #endregion
                //comboBox1.Text.Trim() == "";判断是否为空
            }
            else                                //(string.IsNullOrEmpty(comboBox1.Text.Trim()))
            {
                MessageBox.Show("请选择您的打印机");
                comboBox1.Focus();
            }
        }

        //条形码写入
        private void button8_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                #region 参数变量
                string fenBianLv = "^JM" cbFenBianLv.SelectedItem.ToString();      //  分辨率
                string biaoQianZuoBiao = "^LH" txtBiaoQianX.Text "," txtBiaoQianY.Text;    //标签坐标
                string printStartEndZuoBiao = "^FO" txtPrintX.Text "," txtPrintY.Text;      // 打印起始坐标
                string tiaomaCanshu = "^BY" cbZhaiTiaoKuanDu.Text "," cbBiLi.SelectedItem.ToString() "," txtBarHeight.Text;     //条码宽度、比例和高度
                string ChangeSuDu = "^PR" cbPSD.SelectedItem.ToString() "," cbHSD.SelectedItem.ToString() "," cbFSD.SelectedItem.ToString();     // 打印、回转、反馈速度
                string setZiti = "^A" cbZitiName.SelectedItem.ToString() cbZitiFangXiang.SelectedItem.ToString() "," txtH.SelectedItem.ToString() "," txtW.SelectedItem.ToString();    //字体参数,包括名称、方向、高度和宽度
                string barCodeType = "^BC" cbZhaiTiaoKuanDu.Text "," txtBarHeight.SelectedItem.ToString() "," cbPrintZhuShi.SelectedItem.ToString() "," cbZhuShiHangWeiZhi.SelectedItem.ToString();     //条码编码类型,条码宽度 条码高度,是否打印注释,注释行位置
                string printNeiRong = "^FD" txtPrintNeiRong.Text "^FS";       //打印内容
                #endregion
                textBox1.Text = "^XA" "\t" fenBianLv "\t" biaoQianZuoBiao "\t" printStartEndZuoBiao "\t" tiaomaCanshu "\t" ChangeSuDu "\t" setZiti "\t" barCodeType "\t" printNeiRong "\t" "^XZ";
            }
            else                                //(string.IsNullOrEmpty(comboBox1.Text.Trim()))
            {
                MessageBox.Show("请选择您的打印机");
                comboBox1.Focus();
            }

        }

        //条形码打印
        private void button4_Click(object sender, EventArgs e)
        {
            // 发送一个打印指令到打印机 cmbInstalledPrinters.Text打印机名称, this.richTextBox2.Text(指令)
            RawPrinterHelper.SendStringToPrinter(comboBox1.Text, this.textBox1.Text);
        }


        //保存为模板
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string newTemplate = textBox2.Text ".TXT";
                //【1】创建文件流
                FileStream fs = new FileStream("template\\" newTemplate, FileMode.Create);
                //【2】创建写入器
                StreamWriter sw = new StreamWriter(fs);
                //【3】以流的方式写入数据
                sw.WriteLine(this.textBox1.Text.Trim());
                sw.Close();
                //【5】关闭文件流
                fs.Close();
                //刷新Combox内容
                comboBox3.Items.Add(newTemplate);

            }
            catch (Exception ex)
            {
                MessageBox.Show("There is an IOException" ex.Message);
            }
            MessageBox.Show("Save Succeed!", "验证提示", MessageBoxButtons.YesNoCancel);
        }

        //删除此模板
        private void button6_Click(object sender, EventArgs e)
        {

            File.Delete("Template\\" this.comboBox3.Text.Trim());
            //判断是否删除成功
            if (!File.Exists("Template\\" this.comboBox3.Text.Trim()))
            {
                MessageBox.Show("Delete Success!");
            }
            else { MessageBox.Show("Delete Fail!"); }

            comboBox3.Text = "";
            //刷新Combox内容
            comboBox3.Items.Clear();
            string[] files = Directory.GetFiles("Template");//("D:\\Visual Studio 2019\\VS Project\\Zebra\\ZebarTest1\\Template");
            foreach (string item in files)
            {
                string template = item.ToString();
                string str1 = template.Remove(0, 9);
                //string str1 = template.SubString(8);
                comboBox3.Items.Add(str1);
            }

        }


        //显示时间组件
        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = System.DateTime.Now;
            label35.Text = dt.ToString();

        }


        //二维码写入
        private void button9_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                string fenBianLv = "^JM" cbFenBianLv.SelectedItem.ToString();      //  分辨率
                string biaoQianZuoBiao = "^LH" txtBiaoQianX.Text "," txtBiaoQianY.Text;    //标签坐标
                string printStartEndZuoBiao = "^FO" txtPrintX.Text "," txtPrintY.Text;      // 打印起始坐标
                string printQr = "^BQ" cbQrOrientation.SelectedItem.ToString() "," cbQrModel.SelectedItem.ToString() "," cbQrMagnification.SelectedItem.ToString();       //二维码参数
                string ChangeSuDu = "^PR" cbPSD.SelectedItem.ToString() "," cbHSD.SelectedItem.ToString() "," cbFSD.SelectedItem.ToString();     // 打印、回转、反馈速度
                string setZiti = "^A" cbZitiName.SelectedItem.ToString() cbZitiFangXiang.SelectedItem.ToString() "," txtH.SelectedItem.ToString() "," txtW.SelectedItem.ToString();    //字体参数,包括名称、方向、高度和宽度
                                      //       纠错级别                        输入模式(M, 手动输入   A自动模式)                   字符模式(N 数字数据    A 字母数字数据)              数据字符串
                string printNeiRong = "^FD" cbQrSafe.SelectedItem.ToString() cbQrMaskValue.SelectedItem.ToString() "," txtPrintNeiRong.Text "^FS";       //打印内容#endregion
                textBox1.Text = "^XA" "\t" fenBianLv "\t" biaoQianZuoBiao "\t" printStartEndZuoBiao "\t" printQr "\t" ChangeSuDu "\t" setZiti "\t" printNeiRong "\t" " ^ XZ";
                
            }
            else                                //(string.IsNullOrEmpty(comboBox1.Text.Trim()))
            {
                MessageBox.Show("请选择您的打印机");
                comboBox1.Focus();
            }

        }
        //打印二维码
        private void button7_Click(object sender, EventArgs e)
        {
            RawPrinterHelper.SendStringToPrinter(comboBox1.Text, this.textBox1.Text);
        }

        //打印字符串
        private void button10_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox3.Text))
            {

                string fenBianLv = "^JM" cbFenBianLv.SelectedItem.ToString();      //  分辨率
                string biaoQianZuoBiao = "^LH" txtBiaoQianX.Text "," txtBiaoQianY.Text;    //标签坐标
                string printStartEndZuoBiao = "^FO" txtPrintX.Text "," txtPrintY.Text;      // 打印起始坐标
                string setZiti = "^A" cbZitiName.SelectedItem.ToString() cbZitiFangXiang.SelectedItem.ToString() "," txtH.SelectedItem.ToString() "," txtW.SelectedItem.ToString();    //字体参数,包括名称、方向、高度和宽度
                string printNeiRong = "^FD" txtPrintNeiRong.Text "^FS";       //打印内容
                textBox1.Text = "^XA" "\t" fenBianLv "\t" biaoQianZuoBiao "\t" printStartEndZuoBiao "\t" setZiti "\t" printNeiRong "\t" " ^ XZ";
            }
            else
            {
                MessageBox.Show("请输入你的字符串! ");
            }
        }

标签: 实现 程序 C#

实例下载地址

Zebar打印机 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警