C#实现扫描枪扫描二维码并打印(实例代码)-创新互联

1.使用usb口输入的扫描枪,这里实现使用了winform

成都网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团成都企业网站定制等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:被动防护网等众多领域,积累了大量丰富的经验,同时也获得了客户的一致赞誉!

首先创建一个CS文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace am_sign
{
 class BardCodeHooK
 {
  public delegate void BardCodeDeletegate(BarCodes barCode);
  public event BardCodeDeletegate BarCodeEvent;
  public struct BarCodes
  {
   public int VirtKey;//虚拟吗
   public int ScanCode;//扫描码
   public string KeyName;//键名
   public uint Ascll;//Ascll
   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("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);
  [DllImport("user32", EntryPoint = "GetKeyNameText")]
  private static extern int GetKeyNameText(int IParam, StringBuilder lpBuffer, int nSize);
  [DllImport("user32", EntryPoint = "GetKeyboardState")]
  private static extern int GetKeyboardState(byte[] pbKeyState);
  [DllImport("user32", EntryPoint = "ToAscii")]
  private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeySate, ref uint lpChar, int uFlags);
  delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
  BarCodes barCode = new BarCodes();
  int hKeyboardHook = 0;
  string strBarCode = "";
  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;//扫描码
     StringBuilder strKeyName = new StringBuilder(225);
     if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0)
     {
      barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
     }
     else
     {
      barCode.KeyName = "";
     }
     byte[] kbArray = new byte[256];
     uint uKey = 0;
     GetKeyboardState(kbArray);
     if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0))
     {
      barCode.Ascll = uKey;
      barCode.Chr = Convert.ToChar(uKey);
     }
     TimeSpan ts = DateTime.Now.Subtract(barCode.Time);
     if (ts.TotalMilliseconds > 50)
     {
      strBarCode = barCode.Chr.ToString();
     }
     else
     {
      if ((msg.message & 0xff) == 13 && strBarCode.Length > 3)
      {
       barCode.BarCode = strBarCode;
       barCode.IsValid = true;
      }
      strBarCode += barCode.Chr.ToString();
     }
     barCode.Time = DateTime.Now;
     if (BarCodeEvent != null) BarCodeEvent(barCode);//触发事件
     barCode.IsValid = false;
    }
   }
   return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
  }
  //安装钩子
  public bool Start()
  {
   if (hKeyboardHook == 0)
   {
    //WH_KEYBOARD_LL=13
    hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
   }
   return (hKeyboardHook != 0);
  }
  //卸载钩子
  public bool Stop()
  {
   if (hKeyboardHook != 0)
   {
    return UnhookWindowsHookEx(hKeyboardHook);
   }
   return true;
  }
 }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:C#实现扫描枪扫描二维码并打印(实例代码)-创新互联
文章分享:http://pwwzsj.com/article/egjoc.html