C#實現(xiàn)掃描槍掃描二維碼并打印(實例代碼)-創(chuàng)新互聯(lián)

1.使用usb口輸入的掃描槍,這里實現(xiàn)使用了winform

成都網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信小程序開發(fā)、集團成都企業(yè)網(wǎng)站定制等服務項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務眾多知名企業(yè)客戶;涵蓋的客戶類型包括:被動防護網(wǎng)等眾多領域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致贊譽!

首先創(chuàng)建一個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);//觸發(fā)事件
     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;
  }
 }
}

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)頁名稱:C#實現(xiàn)掃描槍掃描二維碼并打印(實例代碼)-創(chuàng)新互聯(lián)
文章URL:http://muchs.cn/article2/egjoc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、App設計、用戶體驗微信小程序、軟件開發(fā)面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站