C# LPT操作打印条码

  条码打印一般是通过指令或图片方式来打印,图片方式有fastreport,不过本人未曾找到VS调用它的方式,仅在Delphi 7中成功的使用。而实际上大多数的条码打印机制造商都有一套他们自己的打印指令语言,通过该语言,可以无需驱动,直接打印,并且操作也很简单,只需要将指令送入打印机中就好。

  VS中存在Com口操作的控件,却未有现成的LPT端口控件,而相对COM口来说,LPT的速度要快,所以在打印的时候客户一般选择LPT通讯方式,经过网上的一些查阅,终于实现了LPT口的打印,打印机为Zebra,写出来与大家分享。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace PrintDemo
  9. {
  10.     public partial class Form1 : Form
  11.     {  
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.         private void Form1_Load(object sender, EventArgs e)
  17.         {
  18.             tbBarCode.Focus();
  19.         }
  20.         private void tbBarCode_KeyDown(object sender, KeyEventArgs e)
  21.         {
  22.             switch (e.KeyCode)
  23.             {
  24.                 case Keys.Enter:
  25.                     PrintBarcode(tbBarCode.Text.Trim());
  26.                     tbBarCode.Text = "";
  27.                     tbBarCode.Focus();
  28.                     break;
  29.                 default:
  30.                     break;
  31.             }
  32.         }
  33.         private void PrintBarcode(string Barcode)
  34.         {
  35.             Barcode = "^XA^FO48,12^BY4^BCN,152,N,N^FD>;" + Barcode.Trim() + "^FS^FT62,220^CI0^ABN,44,28^FD" + Barcode.Trim() + "^FS^PQ1,0,1,Y^XZ";
  36.             PrintDemo.POSPrinter prn = new PrintDemo.POSPrinter("LPT1");
  37.             string strmsg = prn.PrintLine(Barcode);
  38.             if (strmsg != "")
  39.             {
  40.                 MessageBox.Show(strmsg);
  41.             }
  42.         }
  43.     }
  44. }

 

其中类POSPrinter定义如下

  1. namespace PrintDemo
  2. {
  3.     class POSPrinter
  4.     {
  5.         const int OPEN_EXISTING = 3;
  6.         string prnPort = "LPT1";
  7.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  8.         private static extern IntPtr CreateFile(string lpFileName,
  9.         int dwDesiredAccess,
  10.         int dwShareMode,
  11.         int lpSecurityAttributes,
  12.         int dwCreationDisposition,
  13.         int dwFlagsAndAttributes,
  14.         int hTemplateFile);
  15.         public POSPrinter()
  16.         {
  17.             //   
  18.             //   TODO:   在此处添加构造函数逻辑   
  19.             //   
  20.         }
  21.         public POSPrinter(string prnPort)
  22.         {
  23.             this.prnPort = prnPort;//打印机端口   
  24.         }
  25.         public string PrintLine(string str)
  26.         {
  27.             IntPtr iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
  28.             if (iHandle.ToInt32() == -1)
  29.             {
  30.                 return "LPT1 Port Open Failed";
  31.             }
  32.             else
  33.             {
  34.                 FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
  35.                 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);   //写数据   
  36.                 sw.WriteLine(str);
  37.                 sw.Close();
  38.                 fs.Close();
  39.                 return "";
  40.             }
  41.         }
  42.     }  
  43. }

占华

http://www.cardprinterworld.com

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值