C# LPT操作打印条码

  条码打印一般是通过指令或图片方式来打印,图片方式有fastreport

,不过本人未曾找到VS调用它的方式,仅在Delphi 7中成功的使用。而

实际上大多数的条码打印机制造商都有一套他们自己的打印指令语言,

通过该语言,可以无需驱动,直接打印,并且操作也很简单,只需要将

指令送入打印机中就好。

  VS中存在Com口操作的控件,却未有现成的LPT端口控件,而相对COM

口来说,LPT的速度要快,所以在打印的时候客户一般选择LPT通讯方式

,经过网上的一些查阅,终于实现了LPT口的打印,打印机为Zebra,写

出来与大家分享。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PrintDemo
{
    public partial class Form1 : Form
    {  
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tbBarCode.Focus();
        }

        private void tbBarCode_KeyDown(object sender,

KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    PrintBarcode(tbBarCode.Text.Trim());
                    tbBarCode.Text = "";
                    tbBarCode.Focus();
                    break;
                default:
                    break;
            }
        }
        private void PrintBarcode(string Barcode)
        {
            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";
            PrintDemo.POSPrinter prn = new

PrintDemo.POSPrinter("LPT1");
            string strmsg = prn.PrintLine(Barcode);
            if (strmsg != "")
            {
                MessageBox.Show(strmsg);
            }
        }
    }
}
 

其中类POSPrinter定义如下

namespace PrintDemo
{
    class POSPrinter
    {
        const int OPEN_EXISTING = 3;
        string prnPort = "LPT1";
        [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()
        {
            //   
            //   TODO:   在此处添加构造函数逻辑   
            //   
        }

        public POSPrinter(string prnPort)
        {
            this.prnPort = prnPort;//打印机端口   
        }

        public string PrintLine(string str)
        {

            IntPtr iHandle = CreateFile(prnPort, 0x40000000,

0, 0, OPEN_EXISTING, 0, 0);
            if (iHandle.ToInt32() == -1)
            {
                return "LPT1 Port Open Failed";
            }
            else
            {

                FileStream fs = new FileStream(iHandle,

FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs,

System.Text.Encoding.Default);   //写数据   
                sw.WriteLine(str);
                sw.Close();
                fs.Close();
                return "";
            }
        }
    }  
}

转载于:https://www.cnblogs.com/dashan9zj/archive/2009/05/31/1492690.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值