1. Use the scan gun input from the USB port, here the implementation uses winform
Create a CS file first
using System; using ; using ; using ; using ; using ; namespace am_sign { class BardCodeHooK { public delegate void BardCodeDeletegate(BarCodes barCode); public event BardCodeDeletegate BarCodeEvent; public struct BarCodes { public int VirtKey;//Is it virtual public int ScanCode;//Scan the code public string KeyName;//Key name public uint Ascll;//Ascll public char Chr;//character public string BarCode;//Barcode information public bool IsValid;//Is the barcode valid public DateTime Time;//Scan time } private struct EventMsg { public int message; public int paramL; public int paramH; public int Time; public int hwnd; } [DllImport("", CharSet = , CallingConvention = )] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("", CharSet = , CallingConvention = )] private static extern bool UnhookWindowsHookEx(int idHook); [DllImport("", CharSet = , CallingConvention = )] private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); [DllImport("user32", EntryPoint = "GetKeyNameText")] private static extern int GetKeyNameText(int IParam, StringBuilder lpBuffer, int nSize); [DllImport("user32", EntryPoint = "GetKeyboardState")] private static extern int GetKeyboardState(byte[] pbKeyState); [DllImport("user32", EntryPoint = "ToAscii")] private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeySate, ref uint lpChar, int uFlags); delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); BarCodes barCode = new BarCodes(); int hKeyboardHook = 0; string strBarCode = ""; private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { if (nCode == 0) { EventMsg msg = (EventMsg)(lParam, typeof(EventMsg)); if (wParam == 0x100)//WM_KEYDOWN=0x100 { = & 0xff;//Is it virtual = & 0xff;//Scan the code StringBuilder strKeyName = new StringBuilder(225); if (GetKeyNameText( * 65536, strKeyName, 255) > 0) { = ().Trim(new char[] { ' ', '\0' }); } else { = ""; } byte[] kbArray = new byte[256]; uint uKey = 0; GetKeyboardState(kbArray); if (ToAscii(, , kbArray, ref uKey, 0)) { = uKey; = (uKey); } TimeSpan ts = (); if ( > 50) { strBarCode = (); } else { if (( & 0xff) == 13 && > 3) { = strBarCode; = true; } strBarCode += (); } = ; if (BarCodeEvent != null) BarCodeEvent(barCode);//Trigger event = false; } } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } //Installing the hook public bool Start() { if (hKeyboardHook == 0) { //WH_KEYBOARD_LL=13 hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), (().GetModules()[0]), 0); } return (hKeyboardHook != 0); } //Unload hook public bool Stop() { if (hKeyboardHook != 0) { return UnhookWindowsHookEx(hKeyboardHook); } return true; } } }
2. Call in winform
BardCodeHooK BarCode = new BardCodeHooK(); public Form1() { InitializeComponent(); += new (BarCode_BarCodeEvent); } string value = ""; //value is the content obtained by the code scan gun, ending with Enter private delegate void ShowInfoDelegate( barCode); private void ShowInfo( barCode) { if () { (new ShowInfoDelegate(ShowInfo), new object[] { barCode }); } else { if (("Enter")) { hook_KeyDown(value); value = ""; } else { value += (); } } } void BarCode_BarCodeEvent( barCode) { ShowInfo(barCode); }
3. Before printing, you need to put the file under c:\\windows\system
using ; namespace am_sign { class TSCLIB_DLL { [DllImport("", EntryPoint = "about")] public static extern int about(); [DllImport("", EntryPoint = "openport")] public static extern int openport(string printername); [DllImport("", EntryPoint = "barcode")] public static extern int barcode(string x, string y, string type, string height, string readable, string rotation, string narrow, string wide, string code); [DllImport("", EntryPoint = "clearbuffer")] public static extern int clearbuffer(); [DllImport("", EntryPoint = "closeport")] public static extern int closeport(); [DllImport("", EntryPoint = "downloadpcx")] public static extern int downloadpcx(string filename, string image_name); [DllImport("", EntryPoint = "formfeed")] public static extern int formfeed(); [DllImport("", EntryPoint = "nobackfeed")] public static extern int nobackfeed(); [DllImport("", EntryPoint = "printerfont")] public static extern int printerfont(string x, string y, string fonttype, string rotation, string xmul, string ymul, string text); [DllImport("", EntryPoint = "printlabel")] public static extern int printlabel(string set, string copy); [DllImport("", EntryPoint = "sendcommand")] public static extern int sendcommand(string printercommand); [DllImport("", EntryPoint = "setup")] public static extern int setup(string width, string height, string speed, string density, string sensor, string vertical, string offset); [DllImport("", EntryPoint = "windowsfont")] public static extern int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szFaceName, string content); //Open the printer port and make relevant settings public static void openportExt() { TSCLIB_DLL.openport("Gprinter GP-3120TU");//Look for the printer port TSCLIB_DLL.sendcommand("SIZE 70 mm,50 mm");//Set the barcode size TSCLIB_DLL.sendcommand("GAP 2 mm,0");//Set the barcode gap TSCLIB_DLL.sendcommand("SPEED 5");//Set the printing speed TSCLIB_DLL.sendcommand("DENSITY 8");//Set the ink concentration TSCLIB_DLL.sendcommand("DERECTION 1");//Set relative starting point TSCLIB_DLL.sendcommand("REFERENCE 3 mm,3 mm");//Set offset border TSCLIB_DLL.sendcommand("CLS");//Clear the memory (clear the last printed memory each time you print a new barcode) } //Print in QR code public static void printVehicleCode(string name, string department, string seat_area,string qrcode) { TSCLIB_DLL.sendcommand("CLS");//The last print memory needs to be cleared TSCLIB_DLL.sendcommand("QRCODE 207,180,L,6,A,0,M2,S3,\"" + qrcode + "\""); TSCLIB_DLL.windowsfont(200, 30, 60, 0, 2, 0, "Microsoft YaHei", name); TSCLIB_DLL.windowsfont(210, 100, 30, 0, 0, 0, "Microsoft YaHei", department); TSCLIB_DLL.windowsfont(20, 320, 40, 0, 0, 0, "Microsoft YaHei", seat_area); TSCLIB_DLL.printlabel("1", "1"); } //Close the printer port public static void closeportExt() { TSCLIB_DLL.closeport(); } } }
4. Call Print
TSCLIB_DLL.openportExt(); //Open specified printer driver TSCLIB_DLL.printVehicleCode(“test”, “test”, "test", “QR code content”); TSCLIB_DLL.closeport();
Summarize
The above is the C# implementation scan gun and scan the QR code and print it. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!