一、开发前言:
- 开发工具:vs2015
- 案例性质:winfrom
- 案例引用:ThoughtWorks.QRCode.Codec.dll (主要用来生成二维码)具体使用方法可网上自行搜索
- 开发思路:待生成二维码数据——二维码图片——转成ZPL指令/文字——图片——转成图片——转成ZPL指令
二、效果图
三、代码块
1.winfrom窗体代码
using System;
using System.Drawing.Printing;
namespace DemoPrinter
{
/// <summary>
/// 选择打印机名称
/// </summary>
public partial class FmInstalledPrinters : Form
{
public FmInstalledPrinters()
{
InitializeComponent();
}
private void FmInstalledPrinters_Load(object sender, EventArgs e)
{
#region 绑定打印机名称
foreach (string item in PrinterSettings.InstalledPrinters)//获取安装在本计算机上的所有打印机名称
{
comboBox_printName.Properties.Items.Add(item);
}
if (comboBox_printName.Properties.Items.Contains("ZDesigner GK888t (EPL)"))
{
comboBox_printName.Text = "ZDesigner GK888t (EPL)";
}
#endregion
}
/// <summary>
/// 打印标签按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void simpleButton_print_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(splitMaterialBatch.sm_batch_no))
{
this.Close();
MessageDialog.ShowError("标签打印数据传送失败,请刷新数据后重新操作!");
}
else if (comboBox_printName.Text == "")
{
MessageDialog.ShowError("请选择打印机!");
}
else
{
string text = "";
string qRCodeContent = AssembleData(splitMaterialBatch.sm_batch_no, splitMaterialBatch.sm_work_no, splitMaterialBatch.operator_wn, splitMaterialBatch.material_no, splitMaterialBatch.unit, splitMaterialBatch.weight.ToString(), splitMaterialBatch.sm_date.ToString("yyyy-MM-dd"), splitMaterialBatch.expiry_date.ToString("yyyy-MM-dd"), out text);
//发送打印指令
if (PrintZPL(qRCodeContent, text))
{
this.Close();
MessageDialog.ShowError("标签打印指令发送成功!");
}
else
{
MessageDialog.ShowError("标签打印指令发送失败!");
}
}
}
/// <summary>
/// 取消打印按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void simpleButton_clean_Click(object sender, EventArgs e)
{
this.Close();
}
#region 斑马条码打印机
#region PrintZPL 发送打印指令
/// <summary>
/// 发送打印指令
/// </summary>
/// <param name="printContent">打印内容</param>
private bool PrintZPL(string qRCodeContent, string text)
{
return new SendPrint().MergeSendPrint(comboBox_printName.Text, , text, textBox1);
}
#endregion PrintZPL 发送打印指令
#region AssembleData 组装待打印数据
/// <summary>
/// 组装待打印数据
/// </summary>
/// <returns></returns>
private string AssembleData(out string text)
{
//组装要打印的数据
return qRCodeString;
}
#endregion AssembleData 组装待打印数据
#endregion 斑马条码打印机
}
}
2.发送打印指令
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
namespace DemoPrinter
{
/// <summary>
/// 发送打印指令
/// </summary>
public class SendPrint
{
#region MergeSendPrint 给打印机发送打印数据及打印指令
/// <summary>
/// 给打印机发送打印数据及打印指令
/// </summary>
/// <param name="installedPrinter">连接打印机名称</param>
/// <param name="QRCodeContent">待生成二维码数据</param>
/// <param name="text">显示文字数据</param>
/// <param name="textBox">显示文字数据的font参数传递</param>
public bool MergeSendPrint(string installedPrinter, string qRCodeContent, string text, TextBox textBox)
{
StringBuilder sb = new StringBuilder();
sb.Append("~DGR:SAMPLE.GRF,");
string printString = CreateQrCode(qRCodeContent);
string printString1 = CreateTextBitmap(text, textBox