条码机打印
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Configuration; using System.Xml; namespace QCLPrintBarCode { public partial class Print : Form { public Print() { InitializeComponent(); this.Height = 329;//设置窗口默认的高度 this.Width = 515;//设置窗口的默认宽度 ReadDefault();//读取默认值 InitPrint();//打印 } /// <summary> /// 控件事件的处理 /// </summary> private void InitPrint() { //重置按钮 btnReset.Click += (o, s) => { SaveDefault();//保存修改后的调试信息 }; //展开调试信息按钮 btnOpenTiaoShi.Click += (o, s) => { this.Height = 520; this.Width = 706; }; //收缩调试信息按钮 btnCloseTiaoShi.Click += (o, s) => { this.Height = 329; this.Width = 515; }; cmbBarCodeType.SelectedIndex = 0; cmbPrintType.SelectedIndex = 0; //打印类型(批量打印与单个打印) cmbPrintType.SelectedIndexChanged += (s, e) => { if (cmbPrintType.SelectedIndex == 0) { SinglePanel.Visible = false; BatchPanel.Visible = true; } else { SinglePanel.Visible = true; BatchPanel.Visible = false; } }; //取消打印按钮 btnCancel.Click += (s, e) => { CancelPrint(); }; //打印按钮 btnPrint.Click += (s, e) => { bool flag = ValidateAll(); if (cmbPrintType.SelectedIndex == 0) { flag = (ValiateTextbox(tbxVertical)&&ValiateTextbox(tbxdensity)&&ValiateTextbox(tbxcolumYPos)&&ValiateTextbox(tbxcolumXPos) &&ValiateTextbox(tbxYmul)&&ValiateTextbox(tbxXmul)&&ValiateTextbox(tbxRotation) &&ValiateTextbox(tbxFont)&&ValiateTextbox(txbYPosition) && ValiateTextbox(txbXPosition) && ValiateTextbox(txtLs) && ValiateTextbox(txtFs) && ValiateTextbox(txtBarHeight) && ValiateTextbox(txtMedian)&& ValiateTextbox(txtStarNo) && ValiateTextbox(txtEndNo)); } else { flag = (ValiateTextbox(tbxVertical)&&ValiateTextbox(tbxdensity)&&ValiateTextbox(tbxcolumYPos) &&ValiateTextbox(tbxcolumXPos)&&ValiateTextbox(tbxYmul)&&ValiateTextbox(tbxXmul) &&ValiateTextbox(tbxRotation)&&ValiateTextbox(tbxFont)&&ValiateTextbox(txbYPosition) &&ValiateTextbox(txbXPosition)&&ValiateTextbox(txtLs) && ValiateTextbox(txtFs) && ValiateTextbox(txtBarHeight) && ValiateTextbox(txtMedian)&& ValiateTextbox(txtPrintNo) ); } //条码高度 string height = txtBarHeight.Text.Trim(); //联数 string ls = txtLs.Text.Trim(); //份数 string fs = txtFs.Text.Trim(); //X起始处 string xPos = txbXPosition.Text.Trim(); //Y起始处 string yPos = txbYPosition.Text.Trim(); //字体大小 string font = tbxFont.Text.Trim(); //旋转角度 string rotation = tbxRotation.Text.Trim(); //X方向放大倍数 string xmul = tbxXmul.Text.Trim(); //Y方向放大倍数 string ymul = tbxYmul.Text.Trim(); //列印X方向起始处 string xcolumnPos = tbxcolumXPos.Text.Trim(); //列印Y方向起始处 string columYPos = tbxcolumYPos.Text.Trim(); //列印浓度 string density = tbxdensity.Text.Trim(); //列印垂直间隔 string Vertical = tbxVertical.Text.Trim(); string barCodePrefix = ""; //如果是TCT if (cmbBarCodeType.SelectedIndex == 0) barCodePrefix = "T" + DateTime.Now.ToString("yyyy").Substring(2); //如果是病理 else barCodePrefix = "B" + DateTime.Now.ToString("yyyy").Substring(2); //若验证通过 if (flag == true) { //如果是批量打印 if (cmbPrintType.SelectedIndex == 0) { DialogResult dr = MessageBox.Show( "确认从" + txtStarNo.Text.Trim() + "打印到" + txtEndNo.Text.Trim() + "?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == System.Windows.Forms.DialogResult.Cancel) { flag = false; } if (flag == true) { DialogResult dr2 = MessageBox.Show("确认打印联数为" + txtLs.Text.Trim() + "?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr2 == System.Windows.Forms.DialogResult.Cancel) { flag = false; } } if (flag == true) { int starNo = Convert.ToInt32(txtStarNo.Text.Trim()); int endNo = Convert.ToInt32(txtEndNo.Text.Trim()); for (int i = starNo; i <= endNo; i++) { string allBarCode = barCodePrefix +"-"+ CompletionSerialNumber(i); GoingPrint(xcolumnPos, columYPos, density, Vertical,xPos, yPos, font, rotation, xmul, ymul, height, ls, fs, allBarCode); } } } //若是单个打印 else { string allBarCode = barCodePrefix + "-"+CompletionSerialNumber(Convert.ToInt32(txtPrintNo.Text.Trim())); GoingPrint(xcolumnPos, columYPos, density, Vertical,xPos, yPos, font, rotation, xmul, ymul, height, ls, fs, allBarCode); } } }; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Configuration; using System.Xml; namespace QCLPrintBarCode { public partial class Print : Form { /// <summary> /// 读取保存后的调试信息 /// </summary> private void ReadDefault() { txbXPosition.Text = ConfigurationManager.AppSettings["txbXPosition"]; txbYPosition.Text = ConfigurationManager.AppSettings["txbYPosition"]; tbxFont.Text = ConfigurationManager.AppSettings["tbxFont"]; tbxcolumXPos.Text = ConfigurationManager.AppSettings["tbxcolumXPos"]; tbxcolumYPos.Text = ConfigurationManager.AppSettings["tbxcolumYPos"]; tbxRotation.Text = ConfigurationManager.AppSettings["tbxRotation"]; tbxXmul.Text = ConfigurationManager.AppSettings["tbxXmul"]; tbxYmul.Text = ConfigurationManager.AppSettings["tbxYmul"]; tbxdensity.Text = ConfigurationManager.AppSettings["tbxdensity"]; tbxVertical.Text = ConfigurationManager.AppSettings["tbxVertical"]; rtbCommand.Text = ConfigurationManager.AppSettings["rtbCommand"]; } /// <summary> /// 保存修改后的调试信息 /// </summary> private void SaveDefault() { //ConfigurationManager.AppSettings["txbXPosition"] = txbXPosition.Text.Trim(); UpdateConfig("txbXPosition", txbXPosition.Text.Trim()); UpdateConfig("txbYPosition", txbYPosition.Text.Trim()); UpdateConfig("tbxFont", tbxFont.Text.Trim()); UpdateConfig("tbxcolumXPos", tbxcolumXPos.Text.Trim()); UpdateConfig("tbxcolumYPos", tbxcolumYPos.Text.Trim()); UpdateConfig("tbxRotation", tbxRotation.Text.Trim()); UpdateConfig("tbxXmul", tbxXmul.Text.Trim()); UpdateConfig("tbxYmul", tbxYmul.Text.Trim()); UpdateConfig("tbxdensity", tbxdensity.Text.Trim()); UpdateConfig("tbxVertical", tbxVertical.Text.Trim()); UpdateConfig("rtbCommand", rtbCommand.Text); } /// <summary> /// 文本框验证 /// </summary> /// <param name="str">文本</param> private bool ValiateTextbox(TextBox tb) { bool flag = true; double result; //bool b = int.TryParse(tb.Text.Trim(), out result); try { result = Convert.ToDouble(tb.Text.Trim()); } catch (Exception ex) { flag = false; } //if (!b) //{ // flag = false; //} //else if (result < 0) //{ // flag = false; //} return flag; } /// <summary> /// 验证所有控件 /// </summary> /// <returns></returns> private bool ValidateAll() { bool flag;//验证标志 flag = ValiateTextbox(txtLs); if (!flag) { MessageBox.Show("联数请填写大于0的小数!"); } flag = ValiateTextbox(txtFs); if (!flag) { MessageBox.Show("份数请填写大于0的小数!"); } flag = ValiateTextbox(txtBarHeight); if (!flag) { MessageBox.Show("条码高度请填写大于0的小数!"); } flag = ValiateTextbox(txtMedian); if (!flag) { MessageBox.Show("流水号位数请填写大于0的小数!"); } flag = ValiateTextbox(txbXPosition); if (!flag) { MessageBox.Show("字符串X起始位置请填写大于0的小数!"); } flag = ValiateTextbox(txbYPosition); if (!flag) { MessageBox.Show("字符串Y起始位置请填写大于0的小数!"); } flag = ValiateTextbox(tbxFont); if (!flag) { MessageBox.Show("请选择字体大小!"); } flag = ValiateTextbox(tbxRotation); if (!flag) { MessageBox.Show("旋转角度请填写大于0的小数!"); } flag = ValiateTextbox(tbxXmul); if (!flag) { MessageBox.Show("X放大倍数请填写大于0的小数!"); } flag = ValiateTextbox(tbxYmul); if (!flag) { MessageBox.Show("Y方向倍数请填写大于0的小数!"); } flag = ValiateTextbox(tbxcolumXPos); if (!flag) { MessageBox.Show("列印X方向请填写大于0的小数!"); } flag = ValiateTextbox(tbxcolumYPos); if (!flag) { MessageBox.Show("列印Y方向请填写大于0的小数!"); } flag = ValiateTextbox(tbxdensity); if (!flag) { MessageBox.Show("列印浓度请填写正数!"); } flag = ValiateTextbox(tbxVertical); if (!flag) { MessageBox.Show("列印垂直间隔请填写正数!"); } if (cmbPrintType.SelectedIndex == 0) { flag = ValiateTextbox(txtStarNo); if (!flag) { MessageBox.Show("开始号码请填写大于0的小数!"); } flag = ValiateTextbox(txtEndNo); if (!flag) { MessageBox.Show("结束号码请填写大于0的小数!"); } } else { flag = ValiateTextbox(txtPrintNo); if (!flag) { MessageBox.Show("打印号码请填写大于0的小数!"); } } return flag; } /// <summary> /// 动态修改winform的app.config的键值 /// </summary> /// <param name="p_strKey">key</param> /// <param name="p_strValue">value</param> public static void UpdateConfig(string p_strKey, string p_strValue) { try { string m_strFullPath = ""; // Assembly Asm = Assembly.GetExecutingAssembly(); XmlDocument xmlDoc = new XmlDocument(); m_strFullPath = System.Windows.Forms.Application.ExecutablePath + ".config"; xmlDoc.Load(m_strFullPath); XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes; foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("key").IndexOf(p_strKey) != -1) { xe.SetAttribute("value", p_strValue); } } xmlDoc.Save(m_strFullPath); } catch (System.NullReferenceException NullEx) { throw NullEx; } catch (Exception ex) { throw ex; } } /// <summary> /// 补全流水号 /// </summary> /// <param name="currentNo"></param> /// <returns></returns> private string CompletionSerialNumber(int currentNo) { string No = ""; int Median = Convert.ToInt32(txtMedian.Text.Trim()); for (int i = 0; i < Median - currentNo.ToString().Length; i++) { No += "0"; } return No + currentNo; } /// <summary> /// 取消打印任务 /// </summary> private void CancelPrint() { string Command = rtbCommand.Text; BasePrint.sendcommand(Command); } /// <summary> /// 执行打印 /// </summary> /// <param name="height">条码高度</param> /// <param name="ls">联数</param> /// <param name="fs">份数</param> /// <param name="barCode">打印条码</param> private void GoingPrint(string columXPos, string columYPos, string density,string vertical, string xPos, String yPos, string font, string rotation, string xmul, string ymul, string height, string ls, string fs, string barCode) { BasePrint.openport("TSC TTP-247"); //Open specified printer driver //BasePrint.setup("50", "27", "4", "8", "0", "2", "0"); //Setup the media size and sensor type info BasePrint.setup(columXPos, columYPos, "4", density, "0", vertical, "0"); BasePrint.clearbuffer(); //Clear image buffer //调整打印数字的格式 //BasePrint.printerfont("300", "150", "4", "180", "1", "3", barCode); BasePrint.printerfont(xPos, yPos, font, rotation, xmul, ymul, barCode); // BasePrint.windowsfont(Convert.ToInt32(xPos), Convert.ToInt32(yPos),Convert.ToInt32(font), 180, 2, 0, "Arial", barCode); //BasePrint.barcode("60", "50", "128", height, "1", "0", "2:5", "12", barCode, "2"); //Drawing barcode BasePrint.printlabel(ls, fs); //(默认,打印张数) //Print labels BasePrint.closeport(); } } }