日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#LPT指令打印条码——代码嵌套指令

發布時間:2023/12/29 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#LPT指令打印条码——代码嵌套指令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????? 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 "";
??????????? }
??????? }
??? }??
}

總結

以上是生活随笔為你收集整理的C#LPT指令打印条码——代码嵌套指令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。