欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

无焦点获取条码枪返回值示例

时间:2020-12-29 16:00:35|栏目:.NET代码|点击:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;

namespace BookLibraryManagement.CommonTools
{
    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;
        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;
        }
    }
}

上一篇:asp.net core集成MongoDB的完整步骤

栏    目:.NET代码

下一篇:DropDownList获取的SelectIndex一直为0的问题

本文标题:无焦点获取条码枪返回值示例

本文地址:http://www.codeinn.net/misctech/37864.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有