// 条码扫描器
// 窗体部分相关代码
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
namespace ReadBadCode
{
public partial class frmTest : Form
{
BarCodeHook BarCode = new BarCodeHook();
public frmTest()
{
InitializeComponent();
BarCodeBarCodeEvent += new BarCodeHookBarCodeDelegate(BarCode_BarCodeEvent);
}
private delegate void ShowInfoDelegate(BarCodeHookBarCodes barCode);
private void ShowInfo(BarCodeHookBarCodes barCode)
{
if (thisInvokeRequired)
{
thisBeginInvoke(new ShowInfoDelegate(ShowInfo) new object[] { barCode });
}
else
{
textBoxText = barCodeKeyName;
textBoxText = barCodeVirtKeyToString();
textBoxText = barCodeScanCodeToString();
textBoxText = barCodeAscIIToString();
textBoxText = barCodeChrToString();
textBoxText = barCodeIsValid ? barCodeBarCode : ;
}
}
void BarCode_BarCodeEvent(BarCodeHookBarCodes barCode)
{
ShowInfo(barCode);
}
private void frmTest_Load(object sender EventArgs e)
{
BarCodeStart();
}
private void frmTest_FormClosed(object sender FormClosedEventArgs e)
{
BarCodeStop();
}
private void textBox_TextChanged(object sender EventArgs e)
{
if (textBoxTextLength > )
{
MessageBoxShow(textBoxText);
}
}
}
}
BarCodeHook 类相关代码
using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemRuntimeInteropServices;
using SystemReflection;
namespace ReadBadCode
{
public class BarCodeHook
{
public delegate void BarCodeDelegate(BarCodes barCode);
public event BarCodeDelegate BarCodeEvent;
public struct BarCodes
{
public int VirtKey; //虚拟码
public int ScanCode; //扫描码
public string KeyName; //键名
public uint AscII; //AscII
public char Chr; //字符
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(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]
private static extern int SetWindowsHookEx(int idHook HookProc lpfn IntPtr hInstance int threadId);
[DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);
[DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]
private static extern int CallNextHookEx(int idHook int nCode Int wParam IntPtr lParam);
[DllImport(user EntryPoint = GetKeyNameText)]
private static extern int GetKeyNameText(int lParam StringBuilder lpBuffer int nSize);
[DllImport(user EntryPoint = GetKeyboardState)]
private static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport(user EntryPoint = ToAscii)]
private static extern bool ToAscii(int VirtualKey int ScanCode byte[] lpKeyState ref uint lpChar int uFlags);
delegate int HookProc(int nCode Int wParam IntPtr lParam);
BarCodes barCode = new BarCodes();
int hKeyboardHook = ;
string strBarCode = ;
private int KeyboardHookProc(int nCode Int wParam IntPtr lParam)
{
if (nCode == )
{
EventMsg msg = (EventMsg)MarshalPtrToStructure(lParam typeof(EventMsg));
if (wParam == x) //WM_KEYDOWN = x
{
barCodeVirtKey = ssage & xff; //虚拟码
barCodeScanCode = msgparamL & xff; //扫描码
StringBuilder strKeyName = new StringBuilder();
if (GetKeyNameText(barCodeScanCode * strKeyName ) > )
{
barCodeKeyName = strKeyNameToString()Trim(new char[] { \ });
}
else
{
barCodeKeyName = ;
}
byte[] kbArray = new byte[];
uint uKey = ;
GetKeyboardState(kbArray);
if (ToAscii(barCodeVirtKey barCodeScanCode kbArray ref uKey ))
{
barCodeAscII = uKey;
barCodeChr = ConvertToChar(uKey);
}
if (DateTimeNowSubtract(barCodeTime)TotalMilliseconds > )
{
strBarCode = barCodeChrToString();
}
else
{
if ((ssage & xff) == && strBarCodeLength > ) //回车
{
barCodeBarCode = strBarCode;
barCodeIsValid = true;
}
strBarCode += barCodeChrToString();
}
barCodeTime = DateTimeNow;
if (BarCodeEvent != null) BarCodeEvent(barCode); //触发事件
barCodeIsValid = false;
}
}
return CallNextHookEx(hKeyboardHook nCode wParam lParam);
}
// 安装钩子
public bool Start()
{
if (hKeyboardHook == )
{
//WH_KEYBOARD_LL =
hKeyboardHook = SetWindowsHookEx( new HookProc(KeyboardHookProc) MarshalGetHINSTANCE(AssemblyGetExecutingAssembly()GetModules()[]) );
}
return (hKeyboardHook != );
}
// 卸载钩子
public bool Stop()
{
if (hKeyboardHook != )
{
return UnhookWindowsHookEx(hKeyboardHook);
}
return true;
}
}