熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#操控條形碼掃描槍

2013-11-13 10:00:14  來源: .NET編程 

  // 條碼掃描器

  // 窗體部分相關代碼

  using System;

  using SystemCollectionsGeneric;

  using SystemComponentModel;

  using SystemData;

  using SystemDrawing;

  using SystemText;

  using SystemWindowsForms;

  namespace ReadBadCode

  {

  public partial class frmTest : Form

  {

  BarCodeHook BarCode = new BarCodeHook();

  public frmTest()

  {

  InitializeComponent();

  BarCodeBarCodeEvent += new BarCodeHookBarCodeDelegate(BarCode_BarCodeEvent);

  }

  private delegate void ShowInfoDelegate(BarCodeHookBarCodes barCode);

  private void ShowInfo(BarCodeHookBarCodes barCode)

  {

  if (thisInvokeRequired)

  {

  thisBeginInvoke(new ShowInfoDelegate(ShowInfo) new object[] { barCode });

  }

  else

  {

  textBoxText = barCodeKeyName;

  textBoxText = barCodeVirtKeyToString();

  textBoxText = barCodeScanCodeToString();

  textBoxText = barCodeAscIIToString();

  textBoxText = barCodeChrToString();

  textBoxText = barCodeIsValid ? barCodeBarCode : ;

  }

  }

  void BarCode_BarCodeEvent(BarCodeHookBarCodes barCode)

  {

  ShowInfo(barCode);

  }

  private void frmTest_Load(object sender EventArgs e)

  {

  BarCodeStart();

  }

  private void frmTest_FormClosed(object sender FormClosedEventArgs e)

  {

  BarCodeStop();

  }

  private void textBox_TextChanged(object sender EventArgs e)

  {

  if (textBoxTextLength > )

  {

  MessageBoxShow(textBoxText);

  }

  }

  }

  }

  BarCodeHook  類相關代碼

  using System;

  using SystemCollectionsGeneric;

  using SystemText;

  using SystemRuntimeInteropServices;

  using SystemReflection;

  namespace ReadBadCode

  {

  public class BarCodeHook

  {

  public delegate void BarCodeDelegate(BarCodes barCode);

  public event BarCodeDelegate BarCodeEvent;

  public struct BarCodes

  {

  public int VirtKey;      //虛擬碼

  public int ScanCode;     //掃描碼

  public string KeyName;   //鍵名

  public uint AscII;       //AscII

  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(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  private static extern int SetWindowsHookEx(int idHook HookProc lpfn IntPtr hInstance int threadId);

  [DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  private static extern bool UnhookWindowsHookEx(int idHook);

  [DllImport(userdll CharSet = CharSetAuto CallingConvention = CallingConventionStdCall)]

  private static extern int CallNextHookEx(int idHook int nCode Int wParam IntPtr lParam);

  [DllImport(user EntryPoint = GetKeyNameText)]

  private static extern int GetKeyNameText(int lParam StringBuilder lpBuffer int nSize);

  [DllImport(user EntryPoint = GetKeyboardState)]

  private static extern int GetKeyboardState(byte[] pbKeyState);

  [DllImport(user EntryPoint = ToAscii)]

  private static extern bool ToAscii(int VirtualKey int ScanCode byte[] lpKeyState ref uint lpChar int uFlags);

  delegate int HookProc(int nCode Int wParam IntPtr lParam);

  BarCodes barCode = new BarCodes();

  int hKeyboardHook = ;

  string strBarCode = ;

  private int KeyboardHookProc(int nCode Int wParam IntPtr lParam)

  {

  if (nCode == )

  {

  EventMsg msg = (EventMsg)MarshalPtrToStructure(lParam typeof(EventMsg));

  if (wParam == x)   //WM_KEYDOWN = x

  {

  barCodeVirtKey = ssage & xff;  //虛擬碼

  barCodeScanCode = msgparamL & xff;  //掃描碼

  StringBuilder strKeyName = new StringBuilder();

  if (GetKeyNameText(barCodeScanCode * strKeyName ) > )

  {

  barCodeKeyName = strKeyNameToString()Trim(new char[] { \ });

  }

  else

  {

  barCodeKeyName = ;

  }

  byte[] kbArray = new byte[];

  uint uKey = ;

  GetKeyboardState(kbArray);

  if (ToAscii(barCodeVirtKey barCodeScanCode kbArray ref uKey ))

  {

  barCodeAscII = uKey;

  barCodeChr = ConvertToChar(uKey);

  }

  if (DateTimeNowSubtract(barCodeTime)TotalMilliseconds > )

  {

  strBarCode = barCodeChrToString();

  }

  else

  {

  if ((ssage & xff) == && strBarCodeLength > )   //回車

  {

  barCodeBarCode = strBarCode;

  barCodeIsValid = true;

  }

  strBarCode += barCodeChrToString();

  }

  barCodeTime = DateTimeNow;

  if (BarCodeEvent != null) BarCodeEvent(barCode);    //觸發事件

  barCodeIsValid = false;

  }

  }

  return CallNextHookEx(hKeyboardHook nCode wParam lParam);

  }

  // 安裝鉤子

  public bool Start()

  {

  if (hKeyboardHook == )

  {

  //WH_KEYBOARD_LL =

  hKeyboardHook = SetWindowsHookEx( new HookProc(KeyboardHookProc) MarshalGetHINSTANCE(AssemblyGetExecutingAssembly()GetModules()[]) );

  }

  return (hKeyboardHook != );

  }

  // 卸載鉤子

  public bool Stop()

  {

  if (hKeyboardHook != )

  {

  return UnhookWindowsHookEx(hKeyboardHook);

  }

  return true;

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/12265.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.