斑马打印机测试demo,⼀维码、⼆维码、字符串。可以保存创
建模板以便于后续使⽤
利⽤RawPrinterHelper和Smt.Zebra两种⽅式进⾏打印控制
using Smt.Zebra;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace ZebarTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//底部状态栏结果显⽰
private void ShowResult(int Result)  //  状态栏结果显⽰
{
//TextError.Text = Client.ErrorText(Result);
generic打印机//if (Result == 0)
//    TextError.Text = TextError.Text + "(" + Client.ExecutionTime.ToString() + "ms)";
}
//使⽤斑马打印机打印
private void button1_Click(object sender, EventArgs e)
{
try
{
RawPrinterHelper.SendStringToPrinter(comboBox1.Text, Box1.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】以流的⽅式读取数据
//【4】关闭读取器
sr.Close();
//【5】关闭⽂件流
fs.Close();
//  RawPrinterHelper.SendStringToPrinter(comboBox1.Text, Box1.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.Se                string barCodeType = "^BC" + cbZhaiTiaoKuanDu.Text + "," + txtBarHeight.SelectedItem.ToString() + "," + cbPrintZhuShi.SelectedItem.ToString() + "," +                string printNeiRong = "^FD" + txtPrintNeiRong.Text + "^FS";      //打印内容
#endregion
textBox1.Text = "^XA" + "\t" + fenBianLv + "\t" + biaoQianZuoBiao + "\t" + printStartEndZuoBiao + "\t" + tiaomaCanshu + "\t" + ChangeSuDu + "\t" + setZi            }
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, Box1.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.Box1.Text.Trim());
sw.Close();
//【5】关闭⽂件流
fs.Close();
//刷新Combox内容
comboBox3.Items.Add(newTemplate);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。