110,533
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 返回默认打印机
/// </summary>
/// <returns></returns>
public static string GetDefaultPrinter()
{
System.Drawing.Printing.PrintDocument p = new System.Drawing.Printing.PrintDocument();
return p.DefaultPageSettings.PrinterSettings.PrinterName;
}
namespace ZebraPrintDemo
{
class POSPrinter
{
const int OPEN_EXISTING = 3;
string prnPort = "COM7"; //这里修改连接的口 我是串口的
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(String lpFileName, int dwDesiredAccess, int dwShareMode,
int lpSecurityAttributes, int DwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
public POSPrinter(string prnPort)
{
this.prnPort = prnPort;
}
public string PrintLine(string str)
{
try
{
IntPtr iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
return string.Format("{0} Port Open Failed", prnPort);
else
{
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.WriteLine(str);
sw.Close();
fs.Close();
return "";
}
}
catch (Exception)
{ throw; }
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
List<string> lotID = new List<string>();
lotID.Add("111111111");
lotID.Add("222222222");
lotID.Add("333333333");
lotID.Add("444444444");
lotID.Add("555555555");
lotID.Add("666666666");
lotID.Add("777777777");
lotID.Add("888888888");
lotID.Add("999999999");
lotID.Add("000000000");
PrintBarcode("");
txtLabel.Focus();
txtLabel.SelectAll();
}
private void PrintBarcode(List<string> lotID)
{
string barcode = string.Format(@"CT~~CD,~CC^~CT~
^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ
^XA
^MMT
^PW599
^LL0599
^LS0
^BY2,3,38^FT11,48^BCN,,Y,N
^FD>;{0}^FS
^BY2,3,40^FT248,51^BCN,,Y,N
^FD>;{1}^FS
^BY2,3,47^FT10,128^BCN,,Y,N
^FD>;{2}^FS
^BY2,3,47^FT242,125^BCN,,Y,N
^FD>;{3}^FS
^BY2,3,40^FT12,202^BCN,,Y,N
^FD>;{4}^FS
^BY2,3,47^FT243,205^BCN,,Y,N
^FD>;{5}^FS
^BY2,3,47^FT9,280^BCN,,Y,N
^FD>;{6}^FS
^BY2,3,47^FT249,290^BCN,,Y,N
^FD>;{7}^FS
^BY2,3,40^FT13,371^BCN,,Y,N
^FD>;{8}^FS
^BY2,3,38^FT244,362^BCN,,Y,N
^FD>;{9}^FS
^PQ1,0,1,Y^XZ
", lotID[0],lotID[1],lotID[2],lotID[3],lotID[4],lotID[5],lotID[6],lotID[7],lotID[8],lotID[9]);
POSPrinter prn = new POSPrinter("COM7");
string strmsg = prn.PrintLine(barcode);
if (strmsg != "")
MessageBox.Show(strmsg);
}