本文介绍了阅读使用WPF应用条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 在我的WPF应用程序之一,我有需要能够读取使用C#的条形码值。我不知道如何做到这一点。任何帮助,将不胜感激。 先谢谢了。 PraWiN 解决方案 最好的方法是创建一个键盘钩子。 下面是我在几个项目已经使用了类 您使用它是这样的: VAR挂钩=新的KeyboardHook(); VAR availbleScanners = KeyboardHook.GetKeyboardDevices(); ... //找出使用的扫描仪 hook.SetDeviceFilter(availableScanners.First()); hook.KeyPressed + = OnBarcodeKey; hook.AddHook(YourWPFMainView); ... 公共无效OnBarcodeKey(对象发件人,KeyPressedEventArgs E){ Console.WriteLine(收到+ e.Text); } 我得到了较低水平从这篇文章。 公共类KeyPressedEventArgs:EventArgs的 { 公共KeyPressedEventArgs(字符串文本){ =多行文字文本; } 公共字符串文本{{返回行文字; }} 私人只读字符串行文字; } 公共部分类的KeyboardHook :IDisposable的 {私人静态只读正则表达式DeviceNamePattern =新的正则表达式(@#([^#] + )); 公共事件的EventHandler< KeyPressedEventArgs>则KeyPressed; ///<总结> ///设置设备的键盘钩子使用///< /总结> ///< PARAM NAME =DEVICEID>设备<名称; /参数> ///<退货和GT;真,如果设备被发现与LT; /回报> 公共BOOL SetDeviceFilter(字符串DEVICEID){&字典LT;字符串IntPtr的>器件= FindAllKeyboardDevices(); 返回devices.TryGetValue(DEVICEID,出mHookDeviceId); } ///<总结> ///此的KeyboardHook添加到窗口 ///< /总结> ///&下; PARAM NAME =窗口>将窗口添加到< /参数> 公共无效AddHook(System.Windows.Window窗口){如果(窗口== NULL)抛出新的ArgumentNullException(窗口); 如果(mHwndSource!= NULL)抛出新的InvalidOperationException异常(钩子已经存在); IntPtr的HWND =新WindowInteropHelper(窗口).Handle; mHwndSource = HwndSource.FromHwnd(HWND); 如果(mHwndSource == NULL)抛出新ApplicationException的(无法接收窗口源); mHwndSource.AddHook(的WndProc); RAWINPUTDEVICE [] =摆脱新RAWINPUTDEVICE [1]; 摆脱[0] = .usUsagePage为0x01; 摆脱[0] = .usUsage 0×06; 摆脱[0] = .dwFlags RIDEV_INPUTSINK; 摆脱[0] = .hwndTarget HWND; 如果(!RegisterRawInputDevices(RID,(UINT)rid.Length,(UINT)Marshal.SizeOf(去掉[0])))抛出新ApplicationException的(无法注册原始输入设备)。); } ///<总结> ///从窗口删除这个键盘钩子(如果添加) ///< /总结> 公共无效RemoveHook(){如果(mHwndSource == NULL)的回报; //不是错误 RAWINPUTDEVICE []远离=新RAWINPUTDEVICE [1]; 摆脱[0] = .usUsagePage为0x01; 摆脱[0] = .usUsage 0×06; 摆脱[0] = .dwFlags 00000001; 摆脱[0] = .hwndTarget IntPtr.Zero; RegisterRawInputDevices(RID,(UINT)rid.Length,(UINT)Marshal.SizeOf(去掉[0])); mHwndSource.RemoveHook(的WndProc); mHwndSource.Dispose(); mHwndSource = NULL; } 公共无效的Dispose(){ RemoveHook(); } 私人的IntPtr mHookDeviceId; 私人HwndSource mHwndSource; 私人的IntPtr的WndProc(IntPtr的HWND,INT味精,IntPtr的wParam中,IntPtr的lParam的,裁判布尔处理){开关(味精){情况下WM_INPUT:如果(ProcessInputCommand(mHookDeviceId而言,lParam)){ MSG消息; 的PeekMessage(出消息,IntPtr.Zero,WM_KEYDOWN,WM_KEYDOWN,PM_REMOVE); } 中断; } 返回IntPtr.Zero; } ///<总结> ///获取键盘设备提供 ///℃的名单; /总结> ///<退货和GT;收集和提供LT装置; /回报> 公共静态的ICollection<串GT; GetKeyboardDevices(){返回FindAllKeyboardDevices()键。 } 私有静态字典<字符串IntPtr的> FindAllKeyboardDevices(){&字典LT;字符串IntPtr的> deviceNames =新词典<字符串IntPtr的>(); UINT的deviceCount = 0; INT的dwSize =(Marshal.SizeOf(typeof运算(RAWINPUTDEVICELIST))); 如果(GetRawInputDeviceList(IntPtr.Zero,楼盘的deviceCount,(UINT)的dwSize)== 0){ IntPtr的pRawInputDeviceList = Marshal.AllocHGlobal((INT)(*的dwSize的deviceCount)); 尝试{ GetRawInputDeviceList(pRawInputDeviceList,楼盘的deviceCount,(UINT)的dwSize); 的for(int i = 0; I<的deviceCount;我++){$​​ B $ B UINT pcbSize = 0; VAR摆脱=(RAWINPUTDEVICELIST)Marshal.PtrToStructure(新的IntPtr((pRawInputDeviceList.ToInt32()+(*的dwSize I))), typeof运算(RAWINPUTDEVICELIST)); GetRawInputDeviceInfo(rid.hDevice,RIDI_DEVICENAME,IntPtr.Zero,楼盘pcbSize); 如果(pcbSize大于0){的IntPtr的pData = Marshal.AllocHGlobal((int)的pcbSize); 尝试{ GetRawInputDeviceInfo(rid.hDevice,RIDI_DEVICENAME,pData中,裁判pcbSize); 串DEVICENAME = Marshal.PtrToStringAnsi(pData所); //该列表将包括根键盘和鼠标设备 //这似乎是由终端 //服务或远程桌面使用的远程访问设备 - 我们没有兴趣在这些 //所以下面的代码落入下一个循环迭代如果(deviceName.ToUpper()包含(ROOT)。)继续; //如果设备识别键盘或HID设备, //检查是否是我们要找的中的一个,如果(rid.dwType == RIM_TYPEKEYBOARD || rid.dwType == RIM_TYPEHID){匹配匹配= DeviceNamePattern.Match(DEVICENAME); 如果(match.Success) deviceNames.Add(match.Groups [1] .value的,rid.hDevice); } } 终于{ Marshal.FreeHGlobal(pData所); } } } } 终于{ Marshal.FreeHGlobal(pRawInputDeviceList); } } 返回deviceNames; } ///<总结> ///流程WM_INPUT信息检索有关的任何 ///键盘所发生事件的信息。 ///< /总结> ///< PARAM NAME =DEVICEID>设备来处理与LT; /参数> ///&下; PARAM NAME =lParam的>将WM_INPUT消息处理&所述; /参数> 私人布尔ProcessInputCommand(IntPtr的DEVICEID,IntPtr的lParam的){ UINT的dwSize = 0; 尝试{ //到GetRawInputData首先调用设置的dwSize //的dwSize然后可以用于分配的内存适量,的值//存储在缓冲器的指针。 GetRawInputData(lParam的,RID_INPUT,IntPtr.Zero,楼盘的dwSize,(UINT)Marshal.SizeOf(typeof运算(RAWINPUTHEADER))); IntPtr的缓冲= Marshal.AllocHGlobal((INT)的dwSize); 尝试{ //检查缓冲器指向东西,如果是这样, //再次调用GetRawInputData填充分配存储器 //使用有关输入$ b个信息$ b如果(缓冲= IntPtr.Zero和放大器;!&安培; GetRawInputData(lParam的,RID_INPUT,缓冲,楼盘的dwSize,(UINT)Marshal.SizeOf(typeof运算(RAWINPUTHEADER)))==的dwSize){ //存储在邮件信息的原始,然后检查 //的输入来自前 //键盘设备处理它提出一个适当的keyPressed事件。 RAWINPUT原料=(RAWINPUT)Marshal.PtrToStructure(缓冲的typeof(RAWINPUT)); 如果(raw.header.hDevice = DEVICEID!)返回false; 如果(raw.header.dwType = RIM_TYPEKEYBOARD!)返回FALSE; 如果(raw.keyboard.Message = WM_KEYDOWN和放大器;!&安培;!raw.keyboard.Message = WM_SYSKEYDOWN)返回FALSE; //大多数键盘上,扩展键,如箭头或页面 //键返回两个码 - 密钥本身的代码和一个扩展键标志, //转换到255。该标志不为对我们有用,因此它可以是 //忽略。 如果(raw.keyboard.VKey> VK_LAST_KEY)返回false; 如果(的keyPressed!= NULL){字符串scannedText = NULL; 锁(mLocalBuffer){如果(GetKeyboardState(mKeyboardState)){如果(ToUnicode(raw.keyboard.VKey,raw.keyboard.MakeCode,mKeyboardState,mLocalBuffer,64,0)> ; 0){如果(mLocalBuffer.Length大于0){ scannedText = mLocalBuffer.ToString(); } } } } 如果的keyPressed(这一点,新KeyPressedEventArgs(scannedText))(string.IsNullOrEmpty(scannedText)!); } 返回真; } } 终于{ Marshal.FreeHGlobal(缓冲); } } 赶上(例外错误){ Logger.LogError(ERR扫描仪错误); } 返回FALSE; } 私人静态只读StringBuilder的mLocalBuffer =新的StringBuilder(); 私人静态只读的byte [] mKeyboardState =新的字节[256]; } 公共部分类的KeyboardHook {私人const int的RIDEV_INPUTSINK = 0x00000100; 私人const int的RIDEV_REMOVE = 00000001; 私人const int的RID_INPUT = 0x10000003; 私人const int的FAPPCOMMAND_MASK = 0xF000; 私人const int的FAPPCOMMAND_MOUSE = 0x8000的; 私人const int的FAPPCOMMAND_OEM = 0x1000的; 私人const int的RIM_TYPEMOUSE = 0; 私人const int的RIM_TYPEKEYBOARD = 1; 私人const int的RIM_TYPEHID = 2; 私人const int的RIDI_DEVICENAME = 0x20000007; 私人const int的WM_KEYDOWN = 0100; 私人const int的WM_SYSKEYDOWN = 0x0104; 私人const int的WM_INPUT =设为0x00FF; 私人const int的VK_OEM_CLEAR = 0xFE的; 私人const int的VK_LAST_KEY = VK_OEM_CLEAR; //这是作为一个前哨 私人const int的PM_REMOVE = 0×01一个由值; [StructLayout(LayoutKind.Sequential)] 私人结构R​​AWINPUTDEVICELIST {公众的IntPtr hDevice; [的MarshalAs(UnmanagedType.U4)公众诠释dwType; } [StructLayout(LayoutKind.Explicit)] 私人结构R​​AWINPUT { [FieldOffset(0)] 公共RAWINPUTHEADER头; [FieldOffset(16)] 公共RAWMOUSE鼠标; [FieldOffset(16)] 公共RAWKEYBOARD键盘; [FieldOffset(16)] 公共RAWHID藏; } [StructLayout(LayoutKind.Sequential)] 私人结构R​​AWINPUTHEADER { [的MarshalAs(UnmanagedType.U4)公INT dwType; [的MarshalAs(UnmanagedType.U4)公众诠释的dwSize; 公众的IntPtr hDevice; [的MarshalAs(UnmanagedType.U4)公众诠释的wParam; } [StructLayout(LayoutKind.Sequential)] 私人结构R​​AWHID { [的MarshalAs(UnmanagedType.U4)公INT dwSizHid; [的MarshalAs(UnmanagedType.U4)公众诠释dwCount; } [StructLayout(LayoutKind.Sequential)] 私人结构BUTTONSSTR { [的MarshalAs(UnmanagedType.U2)公USHORT usButtonFlags; [的MarshalAs(UnmanagedType.U2)公共USHORT usButtonData; } [StructLayout(LayoutKind.Explicit)] 私人结构R​​AWMOUSE { [的MarshalAs(UnmanagedType.U2) [ FieldOffset(0)] 公共USHORT usFlags; [的MarshalAs(UnmanagedType.U4) [FieldOffset(4)] 公共UINT ulButtons; [FieldOffset(4)] 公共BUTTONSSTR buttonsStr; [的MarshalAs(UnmanagedType.U4) [FieldOffset(8)] 公共UINT ulRawButtons; [FieldOffset(12)] 公众诠释lLastX; [FieldOffset(16)] 公众诠释lLastY; [的MarshalAs(UnmanagedType.U4) [FieldOffset(20)] 公共UINT ulExtraInformation; } [StructLayout(LayoutKind.Sequential)] 私人结构R​​AWKEYBOARD { [的MarshalAs(UnmanagedType.U2)公USHORT MakeCode; [的MarshalAs(UnmanagedType.U2)公共USHORT标志; [的MarshalAs(UnmanagedType.U2)公共USHORT保留; [的MarshalAs(UnmanagedType.U2)公共USHORT V键; [的MarshalAs(UnmanagedType.U4)公共UINT消息; [的MarshalAs(UnmanagedType.U4)公共UINT ExtraInformation; } [StructLayout(LayoutKind.Sequential)] 私人结构R​​AWINPUTDEVICE { [的MarshalAs(UnmanagedType.U2)公USHORT usUsagePage; [的MarshalAs(UnmanagedType.U2)公共USHORT usUsage; [的MarshalAs(UnmanagedType.U4)公众诠释的dwFlags; 公众的IntPtr hwndTarget; } 函数[DllImport(User32.dll中)] 私人静态外部UINT GetRawInputDeviceList(IntPtr的pRawInputDeviceList,楼盘UINT uiNumDevices,UINT CBSIZE); 函数[DllImport(User32.dll中)] 私人静态外部UINT GetRawInputDeviceInfo(IntPtr的hDevice,UINT uiCommand,IntPtr的pData中,裁判UINT pcbSize); 函数[DllImport(User32.dll中)] 私人静态外部布尔RegisterRawInputDevices(RAWINPUTDEVICE [] pRawInputDevice,UINT uiNumDevices,UINT CBSIZE); 函数[DllImport(User32.dll中)] 私人静态外部UINT GetRawInputData(IntPtr的hRawInput,UINT uiCommand,IntPtr的pData中,裁判UINT pcbSize,UINT cbSizeHeader); 函数[DllImport(user32.dll中,SetLastError = TRUE)] [返回:的MarshalAs(UnmanagedType.Bool)] 私人静态外部布尔GetKeyboardState(字节[] lpKeyState ); 函数[DllImport(user32.dll中)] 私人静态外部INT ToUnicode(UINT wVirtKey,UINT wScanCode,字节[] lpKeyState,[出,的MarshalAs(UnmanagedType.LPWStr,SizeConst = 64)]的StringBuilder pwszBuff, INT cchBuff,UINT wFlags); 函数[DllImport(user32.dll中)] [返回:的MarshalAs(UnmanagedType.Bool)内部静态外部布尔的PeekMessage(出味精LPMSG,IntPtr的HWND,UINT wMsgFilterMin,UINT wMsgFilterMax,UINT wRemoveMsg); } } In one of my WPF applications, I have the need to be able to read bar code values using C#. I am not sure how to do this. Any help would be appreciated.Thanks in advance.PraWiN 解决方案 The best way is to create a keyboard hook.Below is a class that I have used in several projects.You use it like this:var hook = new KeyboardHook();var availbleScanners = KeyboardHook.GetKeyboardDevices();... // find out which scanner to usehook.SetDeviceFilter(availableScanners.First());hook.KeyPressed += OnBarcodeKey;hook.AddHook(YourWPFMainView);...public void OnBarcodeKey(object sender, KeyPressedEventArgs e) { Console.WriteLine("received " + e.Text);}I got the low level keyboard stuff from this article. public class KeyPressedEventArgs : EventArgs { public KeyPressedEventArgs(string text) { mText = text; } public string Text { get { return mText; } } private readonly string mText; } public partial class KeyboardHook : IDisposable { private static readonly Regex DeviceNamePattern = new Regex(@"#([^#]+)"); public event EventHandler<KeyPressedEventArgs> KeyPressed; /// <summary> /// Set the device to use in keyboard hook /// </summary> /// <param name="deviceId">Name of device</param> /// <returns>true if device is found</returns> public bool SetDeviceFilter(string deviceId) { Dictionary<string, IntPtr> devices = FindAllKeyboardDevices(); return devices.TryGetValue(deviceId, out mHookDeviceId); } /// <summary> /// Add this KeyboardHook to a window /// </summary> /// <param name="window">The window to add to</param> public void AddHook(System.Windows.Window window) { if (window == null) throw new ArgumentNullException("window"); if (mHwndSource != null) throw new InvalidOperationException("Hook already present"); IntPtr hwnd = new WindowInteropHelper(window).Handle; mHwndSource = HwndSource.FromHwnd(hwnd); if (mHwndSource == null) throw new ApplicationException("Failed to receive window source"); mHwndSource.AddHook(WndProc); RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1]; rid[0].usUsagePage = 0x01; rid[0].usUsage = 0x06; rid[0].dwFlags = RIDEV_INPUTSINK; rid[0].hwndTarget = hwnd; if (!RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0]))) throw new ApplicationException("Failed to register raw input device(s)."); } /// <summary> /// Remove this keyboard hook from window (if it is added) /// </summary> public void RemoveHook() { if (mHwndSource == null) return; // not an error RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[1]; rid[0].usUsagePage = 0x01; rid[0].usUsage = 0x06; rid[0].dwFlags = 0x00000001; rid[0].hwndTarget = IntPtr.Zero; RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0])); mHwndSource.RemoveHook(WndProc); mHwndSource.Dispose(); mHwndSource = null; } public void Dispose() { RemoveHook(); } private IntPtr mHookDeviceId; private HwndSource mHwndSource; private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case WM_INPUT: if (ProcessInputCommand(mHookDeviceId, lParam)) { MSG message; PeekMessage(out message, IntPtr.Zero, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE); } break; } return IntPtr.Zero; } /// <summary> /// Get a list of keyboard devices available /// </summary> /// <returns>Collection of devices available</returns> public static ICollection<string> GetKeyboardDevices() { return FindAllKeyboardDevices().Keys; } private static Dictionary<string, IntPtr> FindAllKeyboardDevices() { Dictionary<string, IntPtr> deviceNames = new Dictionary<string, IntPtr>(); uint deviceCount = 0; int dwSize = (Marshal.SizeOf(typeof(RAWINPUTDEVICELIST))); if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0) { IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize*deviceCount)); try { GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize); for (int i = 0; i < deviceCount; i++) { uint pcbSize = 0; var rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure( new IntPtr((pRawInputDeviceList.ToInt32() + (dwSize*i))), typeof(RAWINPUTDEVICELIST)); GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize); if (pcbSize > 0) { IntPtr pData = Marshal.AllocHGlobal((int)pcbSize); try { GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, ref pcbSize); string deviceName = Marshal.PtrToStringAnsi(pData); // The list will include the "root" keyboard and mouse devices // which appear to be the remote access devices used by Terminal // Services or the Remote Desktop - we're not interested in these // so the following code with drop into the next loop iteration if (deviceName.ToUpper().Contains("ROOT")) continue; // If the device is identified as a keyboard or HID device, // Check if it is the one we're looking for if (rid.dwType == RIM_TYPEKEYBOARD || rid.dwType == RIM_TYPEHID) { Match match = DeviceNamePattern.Match(deviceName); if (match.Success) deviceNames.Add(match.Groups[1].Value, rid.hDevice); } } finally { Marshal.FreeHGlobal(pData); } } } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } } return deviceNames; } /// <summary> /// Processes WM_INPUT messages to retrieve information about any /// keyboard events that occur. /// </summary> /// <param name="deviceId">Device to process</param> /// <param name="lParam">The WM_INPUT message to process.</param> private bool ProcessInputCommand(IntPtr deviceId, IntPtr lParam) { uint dwSize = 0; try { // First call to GetRawInputData sets the value of dwSize // dwSize can then be used to allocate the appropriate amount of memory, // storing the pointer in "buffer". GetRawInputData(lParam, RID_INPUT, IntPtr.Zero,ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))); IntPtr buffer = Marshal.AllocHGlobal((int)dwSize); try { // Check that buffer points to something, and if so, // call GetRawInputData again to fill the allocated memory // with information about the input if (buffer != IntPtr.Zero && GetRawInputData(lParam, RID_INPUT, buffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dwSize) { // Store the message information in "raw", then check // that the input comes from a keyboard device before // processing it to raise an appropriate KeyPressed event. RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT)); if (raw.header.hDevice != deviceId) return false; if (raw.header.dwType != RIM_TYPEKEYBOARD) return false; if (raw.keyboard.Message != WM_KEYDOWN && raw.keyboard.Message != WM_SYSKEYDOWN) return false; // On most keyboards, "extended" keys such as the arrow or page // keys return two codes - the key's own code, and an "extended key" flag, which // translates to 255. This flag isn't useful to us, so it can be // disregarded. if (raw.keyboard.VKey > VK_LAST_KEY) return false; if (KeyPressed != null) { string scannedText = null; lock (mLocalBuffer) { if (GetKeyboardState(mKeyboardState)) { if (ToUnicode(raw.keyboard.VKey, raw.keyboard.MakeCode, mKeyboardState, mLocalBuffer, 64, 0) > 0) { if (mLocalBuffer.Length > 0) { scannedText = mLocalBuffer.ToString(); } } } } if (!string.IsNullOrEmpty(scannedText)) KeyPressed(this, new KeyPressedEventArgs(scannedText)); } return true; } } finally { Marshal.FreeHGlobal(buffer); } } catch (Exception err) { Logger.LogError(err, "Scanner error"); } return false; } private static readonly StringBuilder mLocalBuffer = new StringBuilder(); private static readonly byte[] mKeyboardState = new byte[256]; } public partial class KeyboardHook { private const int RIDEV_INPUTSINK = 0x00000100; private const int RIDEV_REMOVE = 0x00000001; private const int RID_INPUT = 0x10000003; private const int FAPPCOMMAND_MASK = 0xF000; private const int FAPPCOMMAND_MOUSE = 0x8000; private const int FAPPCOMMAND_OEM = 0x1000; private const int RIM_TYPEMOUSE = 0; private const int RIM_TYPEKEYBOARD = 1; private const int RIM_TYPEHID = 2; private const int RIDI_DEVICENAME = 0x20000007; private const int WM_KEYDOWN = 0x0100; private const int WM_SYSKEYDOWN = 0x0104; private const int WM_INPUT = 0x00FF; private const int VK_OEM_CLEAR = 0xFE; private const int VK_LAST_KEY = VK_OEM_CLEAR; // this is a made up value used as a sentinal private const int PM_REMOVE = 0x01; [StructLayout(LayoutKind.Sequential)] private struct RAWINPUTDEVICELIST { public IntPtr hDevice; [MarshalAs(UnmanagedType.U4)] public int dwType; } [StructLayout(LayoutKind.Explicit)] private struct RAWINPUT { [FieldOffset(0)] public RAWINPUTHEADER header; [FieldOffset(16)] public RAWMOUSE mouse; [FieldOffset(16)] public RAWKEYBOARD keyboard; [FieldOffset(16)] public RAWHID hid; } [StructLayout(LayoutKind.Sequential)] private struct RAWINPUTHEADER { [MarshalAs(UnmanagedType.U4)] public int dwType; [MarshalAs(UnmanagedType.U4)] public int dwSize; public IntPtr hDevice; [MarshalAs(UnmanagedType.U4)] public int wParam; } [StructLayout(LayoutKind.Sequential)] private struct RAWHID { [MarshalAs(UnmanagedType.U4)] public int dwSizHid; [MarshalAs(UnmanagedType.U4)] public int dwCount; } [StructLayout(LayoutKind.Sequential)] private struct BUTTONSSTR { [MarshalAs(UnmanagedType.U2)] public ushort usButtonFlags; [MarshalAs(UnmanagedType.U2)] public ushort usButtonData; } [StructLayout(LayoutKind.Explicit)] private struct RAWMOUSE { [MarshalAs(UnmanagedType.U2)] [FieldOffset(0)] public ushort usFlags; [MarshalAs(UnmanagedType.U4)] [FieldOffset(4)] public uint ulButtons; [FieldOffset(4)] public BUTTONSSTR buttonsStr; [MarshalAs(UnmanagedType.U4)] [FieldOffset(8)] public uint ulRawButtons; [FieldOffset(12)] public int lLastX; [FieldOffset(16)] public int lLastY; [MarshalAs(UnmanagedType.U4)] [FieldOffset(20)] public uint ulExtraInformation; } [StructLayout(LayoutKind.Sequential)] private struct RAWKEYBOARD { [MarshalAs(UnmanagedType.U2)] public ushort MakeCode; [MarshalAs(UnmanagedType.U2)] public ushort Flags; [MarshalAs(UnmanagedType.U2)] public ushort Reserved; [MarshalAs(UnmanagedType.U2)] public ushort VKey; [MarshalAs(UnmanagedType.U4)] public uint Message; [MarshalAs(UnmanagedType.U4)] public uint ExtraInformation; } [StructLayout(LayoutKind.Sequential)] private struct RAWINPUTDEVICE { [MarshalAs(UnmanagedType.U2)] public ushort usUsagePage; [MarshalAs(UnmanagedType.U2)] public ushort usUsage; [MarshalAs(UnmanagedType.U4)] public int dwFlags; public IntPtr hwndTarget; } [DllImport("User32.dll")] private static extern uint GetRawInputDeviceList(IntPtr pRawInputDeviceList, ref uint uiNumDevices, uint cbSize); [DllImport("User32.dll")] private static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize); [DllImport("User32.dll")] private static extern bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize); [DllImport("User32.dll")] private static extern uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetKeyboardState(byte[] lpKeyState); [DllImport("user32.dll")] private static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)] StringBuilder pwszBuff, int cchBuff, uint wFlags); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool PeekMessage(out MSG lpmsg, IntPtr hwnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg); }} 这篇关于阅读使用WPF应用条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 21:30