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

开发 后端
C#打印条码操作的具体实现是如何的呢?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. }  

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

【编辑推荐】

  1. C#打印文本文件实例详解
  2. C#打印设置实例解析
  3. C#Lpt端口打印类的操作浅析
  4. C#打印设置实现源码详解
  5. C#打印控件的使用实例浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-26 13:36:33

C#打印控件

2009-08-18 13:49:21

C# 操作Excel

2009-08-19 10:25:14

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-31 18:38:59

C#写文件

2009-08-18 16:04:12

C# 操作Excel

2009-08-19 09:42:52

C#操作Word书签

2009-08-19 11:28:41

C#操作Word

2009-08-20 11:07:07

C#共享内存

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-18 16:49:05

C# 操作XML

2009-08-27 13:30:11

C# interfac

2009-08-18 17:42:12

C#操作符重载

2009-08-19 16:30:55

C#操作Access数

2009-08-19 14:12:23

C#操作注册表

2009-08-26 14:03:26

C#打印原理

2009-08-19 17:44:15

C#操作文本文件

2009-08-27 17:59:56

C#接口定义

2009-08-17 13:34:02

C#异步操作
点赞
收藏

51CTO技术栈公众号