Csharp/C#教程:无焦点获取条码枪返回值示例分享

代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Runtime.InteropServices;
usingSystem.Reflection;

namespaceBookLibraryManagement.CommonTools
{
   classBarCodeHook
   {
       publicdelegatevoidBarCodeDelegate(BarCodesbarCode);
       publiceventBarCodeDelegateBarCodeEvent;

       publicstructBarCodes
       {
           publicintVirtKey;     //虚拟码 
           publicintScanCode;    //扫描码 

           publicstringBarCode;  //条码信息 
           publicboolIsValid;    //条码是否有效 
           publicDateTimeTime;   //扫描时间 
       }

       privatestructEventMsg
       {
           publicintmessage;
           publicintparamL;
           publicintparamH;
           publicintTime;
           publicinthwnd;
       }

       [DllImport(“user32.dll”,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
       privatestaticexternintSetWindowsHookEx(intidHook,HookProclpfn,IntPtrhInstance,intthreadId);

       [DllImport(“user32.dll”,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
       privatestaticexternboolUnhookWindowsHookEx(intidHook);

       [DllImport(“user32.dll”,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
       privatestaticexternintCallNextHookEx(intidHook,intnCode,Int32wParam,IntPtrlParam);

       delegateintHookProc(intnCode,Int32wParam,IntPtrlParam);

       BarCodesbarCode=newBarCodes();
       inthKeyboardHook=0;
       List<char>_barcode=newList<char>(100);
       privateintKeyboardHookProc(intnCode,Int32wParam,IntPtrlParam)
       {
           if(nCode==0)
           {
               EventMsgmsg=(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=newString(_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));
               }
           }
           returnCallNextHookEx(hKeyboardHook,nCode,wParam,lParam);
       }

       privatestaticHookProchookproc;
       //安装钩子  
       publicboolStart()
       {
           if(hKeyboardHook==0)
           {
               hookproc=newHookProc(KeyboardHookProc);
               //WH_KEYBOARD_LL=13 
               hKeyboardHook=SetWindowsHookEx(13,hookproc,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
           }
           return(hKeyboardHook!=0);
       }

       //卸载钩子  
       publicboolStop()
       {
           if(hKeyboardHook!=0)
           {
               returnUnhookWindowsHookEx(hKeyboardHook);
           }
           returntrue;
       }
   }
}

您可能感兴趣的文章:C#实现的ZPL条码打印类完整实例asp.net(C#)生成Code39条形码实例条码枪可以扫描出ASP.net(c#)生成条形码code39条码生成方法支付宝支付开发——当面付条码支付和扫码支付实例基于Bootstrap的Metronic框架实现条码和二维码的生成及打印处理操作jQuery插件jquery-barcode实现条码打印的方法Google二维条码API整理C#条码生成及打印实例代码

标签: 返回值 示例

c#制作屏幕保护程序步骤(字幕屏保)

C++采用TLS线程局部存储的用法实例

上述就是C#学习教程:无焦点获取条码枪返回值示例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/905189.html

(0)
上一篇 2021年10月22日
下一篇 2021年10月22日

精彩推荐