winfrom钩子实现无焦点扫码输入条码获取-最容易看懂的Demo
在网上找了很长时间,都没有很简单的能看懂的Demo,因为工作需要,我自己完成一个简单的Demo,保证傻瓜都能看懂。
- 钩子类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ScanDemo
{
class BarCodeHook
{
public delegate void BarCodeDelegate(BarCodes barCode);
public event BarCodeDelegate BarCodeEvent;
public struct BarCodes
{
public int VirtKey; //虚拟码
public int ScanCode; //扫描码
public string BarCode; //条码信息
public bool IsValid; //条码是否有效
public DateTime Time; //扫描时间
}
private struct EventMsg //事件信息
{
public int message;
public int paramL;
public int paramH;
public int Time;
public int hwnd;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
BarCodes barCode = new BarCodes();
int hKeyboardHook = 0;
public List<char> _barcode = new List<char>(100);
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (nCode == 0)
{
EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));
if (wParam == 0x100) //WM_KEYDOWN = 0x100
{
barCode.VirtKey = msg.message & 0xff; //虚拟码
barCode.ScanCode = msg.paramL & 0xff; //扫描码
if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 100)
{
_barcode.Clear();
}
else
{
if ((msg.message & 0xff) == 13 && _barcode.Count > 0) //回车
{
barCode.BarCode = new String(_barcode.ToArray());
barCode.IsValid = true;
_barcode.Clear();
}
}
barCode.Time = DateTime.Now;
if (BarCodeEvent != null) BarCodeEvent(barCode); //触发事件
barCode.IsValid = false;
_barcode.Add(Convert.ToChar(msg.message & 0xff));
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
private static HookProc hookproc;
// 安装钩子
public bool Start()
{
if (hKeyboardHook == 0)
{
hookproc = new HookProc(KeyboardHookProc);
//WH_KEYBOARD_LL = 13
hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
}
return (hKeyboardHook != 0);
}
// 卸载钩子
public bool Stop()
{
if (hKeyboardHook != 0)
{
return UnhookWindowsHookEx(hKeyboardHook);
}
return true;
}
}
}
2.在winform界面展示
3.给出界面应用的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScanDemo
{
public partial class Form1 : Form
{
//钩子
BarCodeHook BarCode = new BarCodeHook();
public Form1()
{
BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);//扫码委托
InitializeComponent();
}
private delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);
private void HandleBarCode(BarCodeHook.BarCodes barCode)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowInfoDelegate(HandleBarCode), new object[] { barCode });
}
else
{
if (barCode.IsValid)
{
this.label1.Text = Regex.Replace(barCode.BarCode, @"\s", "");//这里使用了一个正则,为了去掉里面的空格,制表符等无效字符
}
}
}
//钩子回调方法
void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
{
HandleBarCode(barCode);
}
private void button1_Click(object sender, EventArgs e)
{
BarCode.Start();//钩子安装
}
private void button2_Click(object sender, EventArgs e)
{
BarCode.Stop();//钩子卸载
}
}
}
4.效果