条码打印
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace QCLPrintBarCode
{
public partial class Print : Form
{
public Print()
{
InitializeComponent();
InitPrint();
}
private void InitPrint()
{
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;
}
};
btnPrint.Click += (s, e) => {
string height = txtBarHeight.Text.Trim();
string ls = txtLs.Text.Trim();
string fs = txtFs.Text.Trim();
string barCodePrefix = "";
//如果是TCT
if (cmbBarCodeType.SelectedIndex == 0)
barCodePrefix = "T" + DateTime.Now.ToString("yyyyMMdd");
else
barCodePrefix = "T" + DateTime.Now.ToString("yyyyMMdd");
if (cmbPrintType.SelectedIndex == 0)
{
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(height, ls, fs, allBarCode);
}
}
else
{
string allBarCode = barCodePrefix + CompletionSerialNumber(Convert.ToInt32(txtPrintNo.Text.Trim()));
GoingPrint(height, ls, fs, allBarCode);
}
};
}
/// <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;
}
private void GoingPrint(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.clearbuffer(); //Clear image buffer
BasePrint.barcode("60", "50", "128", height, "1", "0", "2:5", "12", barCode, "2"); //Drawing barcode
BasePrint.printlabel(ls, fs); //(默认,打印张数) //Print labels
BasePrint.closeport();
}
}
}