C# LPT操作列印條碼

來源:互聯網
上載者:User

  條碼列印一般是通過指令或圖片方式來列印,圖片方式有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 "";
            }
        }
    }  
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.