RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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. private void Form1_Load(object sender, EventArgs e) 
  18. { 
  19. tbBarCode.Focus(); 
  20. } 
  21. //C#打印条码操作的实例
  22. private void tbBarCode_KeyDown(object sender, 
  23. KeyEventArgs e) 
  24. { 
  25. switch (e.KeyCode) 
  26. { 
  27. case Keys.Enter: 
  28. PrintBarcode(tbBarCode.Text.Trim()); 
  29. tbBarCode.Text = ""; 
  30. tbBarCode.Focus(); 
  31. break; 
  32. default: 
  33. break; 
  34. } 
  35. } 
  36. private void PrintBarcode(string Barcode) 
  37. { 
  38. Barcode = "^XA^FO48,12^BY4^BCN,152,N,N^FD>;" + 
  39. //C#打印条码操作的实例
  40. Barcode.Trim() + "^FS^FT62,220^CI0^ABN,44,28^FD" + 
  41. Barcode.Trim() + "^FS^PQ1,0,1,Y^XZ"; 
  42. PrintDemo.POSPrinter prn = new 
  43. PrintDemo.POSPrinter("LPT1"); 
  44. string strmsg = prn.PrintLine(Barcode); 
  45. if (strmsg != "") 
  46. { 
  47. MessageBox.Show(strmsg); 
  48. } 
  49. } 
  50. } 
  51. } 

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

C#打印条码操作的实例浅析就向你介绍到这里,希望对你了解和学习C#打印条码操作有所了解。


本文名称:C#打印条码操作的实例浅析
本文来源:http://www.zcwtytd.com/article/coiejss.html