C# 红外条码扫描器的另类使用

来源:互联网 发布:智慧城市与云计算 编辑:程序博客网 时间:2024/04/20 10:06

转载自:http://blog.csdn.net/yefanqiu/article/details/4146580

【目前的条形码扫描器有点类似外接键盘(其实从消息传送上它就相当于一个键盘),把输入焦点定位到可输入的控件上,一扫描相应的条形码信息就输入到文本框中去了,但是如果没有输入焦点,或另一个不相干的程序获得输入焦点,那就有点乱套了。我想实现的是,不管什么情况,只要扫描器一工作,我的程序就能自动激活,并能获得当前输入的条形码信息。 实现思路:我用的是litele牌的USB口的红外条形码扫描器,仔细分析了一下,扫描成功后,以键盘按键消息的形式把条形码输入信息通知给系统。这样通过键盘钩子就可以方便的获得该信息了。但是,怎样区分信息是键盘还是条形码输入的哪?很简单,条形码扫描器在很短的时间内输入了至少3个字符以上信息,并且以“回车”作为结束字符,在这种思想指引下,很完美的实现了预定功能。】

     VB相关的代码请见:http://blog.csdn.net/yefanqiu/archive/2006/08/30/1144881.aspx

    

    

 

 


 

窗体相关代码:

[c-sharp] view plaincopy
  1.  using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace ReadBadCode  
  10. {  
  11.     public partial class frmTest : Form  
  12.     {  
  13.         BarCodeHook BarCode = new BarCodeHook();  
  14.         public frmTest()  
  15.         {  
  16.             InitializeComponent();  
  17.             BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);  
  18.         }  
  19.   
  20.         private delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);  
  21.         private void ShowInfo(BarCodeHook.BarCodes barCode)  
  22.         {  
  23.             if (this.InvokeRequired)  
  24.             {  
  25.                 this.BeginInvoke(new ShowInfoDelegate(ShowInfo), new object[] { barCode });  
  26.             }  
  27.             else  
  28.             {  
  29.                 textBox1.Text = barCode.KeyName;  
  30.                 textBox2.Text = barCode.VirtKey.ToString();  
  31.                 textBox3.Text = barCode.ScanCode.ToString();  
  32.                 textBox4.Text = barCode.AscII.ToString();  
  33.                 textBox5.Text = barCode.Chr.ToString();  
  34.                 textBox6.Text = barCode.IsValid ? barCode.BarCode : "";  
  35.             }  
  36.         }  
  37.   
  38.         void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)  
  39.         {  
  40.             ShowInfo(barCode);  
  41.         }  
  42.   
  43.         private void frmTest_Load(object sender, EventArgs e)  
  44.         {  
  45.             BarCode.Start();  
  46.         }  
  47.   
  48.         private void frmTest_FormClosed(object sender, FormClosedEventArgs e)  
  49.         {  
  50.             BarCode.Stop();  
  51.         }  
  52.   
  53.         private void textBox6_TextChanged(object sender, EventArgs e)  
  54.         {  
  55.             if (textBox6.Text.Length > 0)  
  56.             {  
  57.                 MessageBox.Show(textBox6.Text);  
  58.             }  
  59.         }  
  60.     }  
  61. }  
  62.   
  63.    

 

BarCodeHook 类:

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5. using System.Reflection;  
  6.   
  7. namespace ReadBadCode  
  8. {  
  9.     public class BarCodeHook  
  10.     {  
  11.         public delegate void BarCodeDelegate(BarCodes barCode);  
  12.         public event BarCodeDelegate BarCodeEvent;  
  13.   
  14.         public struct BarCodes  
  15.         {  
  16.             public int VirtKey;      //虚拟码  
  17.             public int ScanCode;     //扫描码  
  18.             public string KeyName;   //键名  
  19.             public uint AscII;       //AscII  
  20.             public char Chr;         //字符  
  21.   
  22.             public string BarCode;   //条码信息  
  23.             public bool IsValid;     //条码是否有效  
  24.             public DateTime Time;    //扫描时间  
  25.         }  
  26.   
  27.         private struct EventMsg  
  28.         {  
  29.             public int message;  
  30.             public int paramL;  
  31.             public int paramH;  
  32.             public int Time;  
  33.             public int hwnd;  
  34.         }  
  35.          
  36.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]  
  37.         private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);  
  38.   
  39.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]  
  40.         private static extern bool UnhookWindowsHookEx(int idHook);  
  41.   
  42.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]  
  43.         private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);  
  44.   
  45.         [DllImport("user32", EntryPoint = "GetKeyNameText")]  
  46.         private static extern int GetKeyNameText(int lParam, StringBuilder lpBuffer, int nSize);  
  47.   
  48.         [DllImport("user32", EntryPoint = "GetKeyboardState")]  
  49.         private static extern int GetKeyboardState(byte[] pbKeyState);  
  50.   
  51.         [DllImport("user32", EntryPoint = "ToAscii")]  
  52.         private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeyState, ref uint lpChar, int uFlags);  
  53.   
  54.         delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);  
  55.         BarCodes barCode = new BarCodes();  
  56.         int hKeyboardHook = 0;  
  57.         string strBarCode = "";  
  58.   
  59.         private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)  
  60.         {  
  61.             if (nCode == 0)  
  62.             {  
  63.                 EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));  
  64.   
  65.                 if (wParam == 0x100)   //WM_KEYDOWN = 0x100  
  66.                 {  
  67.                     barCode.VirtKey = msg.message & 0xff;  //虚拟码  
  68.                     barCode.ScanCode = msg.paramL & 0xff;  //扫描码  
  69.   
  70.                     StringBuilder strKeyName = new StringBuilder(255);  
  71.                     if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0)  
  72.                     {  
  73.                         barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ''/0' });  
  74.                     }  
  75.                     else  
  76.                     {  
  77.                         barCode.KeyName = "";  
  78.                     }  
  79.   
  80.                     byte[] kbArray = new byte[256];  
  81.                     uint uKey = 0;  
  82.                     GetKeyboardState(kbArray);                                          
  83.                     if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0))  
  84.                     {  
  85.                         barCode.AscII = uKey;  
  86.                         barCode.Chr = Convert.ToChar(uKey);  
  87.                     }  
  88.   
  89.                     if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 50)   
  90.                     {  
  91.                         strBarCode = barCode.Chr.ToString();  
  92.                     }  
  93.                     else  
  94.                     {  
  95.                         if ((msg.message & 0xff) == 13 && strBarCode.Length > 3)   //回车  
  96.                         {  
  97.                             barCode.BarCode = strBarCode;  
  98.                             barCode.IsValid = true;  
  99.                         }  
  100.                         strBarCode += barCode.Chr.ToString();  
  101.                     }  
  102.   
  103.                     barCode.Time = DateTime.Now;  
  104.                     if (BarCodeEvent != null) BarCodeEvent(barCode);    //触发事件  
  105.                     barCode.IsValid = false;  
  106.                 }  
  107.             }  
  108.             return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);            
  109.         }  
  110.           
  111.         // 安装钩子   
  112.         public bool Start()  
  113.         {  
  114.             if (hKeyboardHook == 0)  
  115.             {  
  116.                 //WH_KEYBOARD_LL = 13  
  117.                 hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);  
  118.             }  
  119.             return (hKeyboardHook != 0);  
  120.         }  
  121.   
  122.         // 卸载钩子   
  123.         public bool Stop()  
  124.         {  
  125.             if (hKeyboardHook != 0)  
  126.             {  
  127.                 return UnhookWindowsHookEx(hKeyboardHook);  
  128.             }  
  129.             return true;  
  130.         }  
  131.     }  

【注意】和VB程序不同,要想测试实际的效果,必须执行编译后的Exe文件,在开发环境直接运行会没有效果的。

原创粉丝点击