C#打印条码操作的实例浅析

C#打印条码一般是通过图片方式或指令方式来打印,图片形式主要有fastreport。这里我们使用LPT端口控件来实现,而实际上绝大多数的条码打印机厂商都有一套他们自己的打印指令语言,通过这种语言,可以不需要驱动,支持直接打印,并且操作简单,仅仅将指令送入打印机中就好。

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

C#打印条码操作的实例:

 
 
  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. //C#打印条码操作的实例  
  9. namespace PrintDemo   
  10. {   
  11. public partial class Form1 : Form   
  12. {     
  13. public Form1()   
  14. {   
  15. InitializeComponent();   
  16. }   
  17.  
  18. private void Form1_Load(object sender, EventArgs e)   
  19. {   
  20. tbBarCode.Focus();   
  21. }   
  22. //C#打印条码操作的实例  
  23. private void tbBarCode_KeyDown(object sender,   
  24.  
  25. KeyEventArgs e)   
  26. {   
  27. switch (e.KeyCode)   
  28. {   
  29. case Keys.Enter:   
  30. PrintBarcode(tbBarCode.Text.Trim());   
  31. tbBarCode.Text = "";   
  32. tbBarCode.Focus();   
  33. break;   
  34. default:   
  35. break;   
  36. }   
  37. }   
  38. private void PrintBarcode(string Barcode)   
  39. {   
  40. Barcode = "^XA^FO48,12^BY4^BCN,152,N,N^FD>;" +   
  41. //C#打印条码操作的实例  
  42. Barcode.Trim() + "^FS^FT62,220^CI0^ABN,44,28^FD" +   
  43.  
  44. Barcode.Trim() + "^FS^PQ1,0,1,Y^XZ";   
  45. PrintDemo.POSPrinter prn = new   
  46.  
  47. PrintDemo.POSPrinter("LPT1");   
  48. string strmsg = prn.PrintLine(Barcode);   
  49. if (strmsg != "")   
  50. {   
  51. MessageBox.Show(strmsg);   
  52. }   
  53. }   
  54. }   
  55. }   

C#打印条码操作之类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   
  9.  
  10. lpFileName,   
  11. int dwDesiredAccess,   
  12. int dwShareMode,   
  13. int lpSecurityAttributes,   
  14. int dwCreationDisposition,   
  15. int dwFlagsAndAttributes,   
  16. int hTemplateFile);   
  17.  
  18. public POSPrinter()   
  19. {   
  20. //  
  21. //   TODO:   在此处添加构造函数逻辑  
  22. //  
  23. }   
  24.  
  25. public POSPrinter(string prnPort)   
  26. {   
  27. this.prnPort = prnPort;//打印机端口  
  28. }   
  29.  
  30. public string PrintLine(string str)   
  31. {   
  32.  
  33. IntPtr iHandle = CreateFile(prnPort, 0x40000000,   
  34.  
  35. 0, 0, OPEN_EXISTING, 0, 0);   
  36. if (iHandle.ToInt32() == -1)   
  37. {   
  38. return "LPT1 Port Open Failed";   
  39. }   
  40. else   
  41. {   
  42.  
  43. FileStream fs = new FileStream(iHandle,   
  44.  
  45. FileAccess.ReadWrite);   
  46. StreamWriter sw = new StreamWriter(fs,   
  47.  
  48. System.Text.Encoding.Default);//C#打印条码操作之写数据  
  49. sw.WriteLine(str);   
  50. sw.Close();   
  51. fs.Close();   
  52. return "";   
  53. }   
  54. }   
  55. }     
  56. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值