自定义的BarCode,封装进行斑马打印的信息

来源:互联网 发布:编程大赛题目 编辑:程序博客网 时间:2024/06/02 17:40
//必须将fnthex32.dll 与执行文件放在同一路径下(....\bin\....),无需添加引用. 将项目以类库的形式生成.那么在外部只需添加此类库,//调用方法进行打印即可.using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Runtime.InteropServices;namespace BarCode{ public class BarCode { [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] private struct OVERLAPPED { int Internal; int InternalHigh; int Offset; int OffSetHigh; int hEvent; } [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped); [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern bool CloseHandle(int hObject); private static int iHandle; public static bool Open() { iHandle = CreateFile("LPT1:", (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 0, 0); if (iHandle != -1) { return true; } else { return false; } } public static bool Write(string Mystring) { if (iHandle != -1) { int i; OVERLAPPED x; byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring); return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x); } else { throw new Exception("LPT1端口未打开!"); } } public static bool Close() { return CloseHandle(iHandle); } [DllImport("fnthex32.dll")] public static extern int GETFONTHEX( string BarcodeText, string FontName, string FileName, int Orient,//方向 int Height, int Width, int IsBold, int IsItalic, StringBuilder ReturnBarcodeCMD); }}
0 0
原创粉丝点击