目前的條形碼掃描器有點類似外接鍵盤(其實從訊息傳送上它就相當于一個鍵盤),把輸入焦點定位到可輸入的控制元件上,一掃描相應的條形碼資訊就輸入到文本框中去了,但是如果沒有輸入焦點,或另一個不相干的程式獲得輸入焦點,那就有點亂套了。我想實作的是,不管什么情況,只要掃描器一作業,我的程式就能自動激活,并能獲得當前輸入的條形碼資訊。實作思路:我用的USB口的條形碼掃描器,仔細分析了一下,掃描成功后,以鍵盤按鍵訊息的形式把條形碼輸入資訊通知給系統。這樣通過鍵盤鉤子就可以方便的獲得該資訊了。但是,怎樣區分資訊是鍵盤還是條形碼輸入的哪?很簡單,條形碼掃描器在很短的時間內輸入了至少3個字符以上資訊,并且以“回車”作為結束字符,在這種思想指引下,很完美的實作了預定功能。
表單相關代碼:
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
usingSystem.Windows.Forms;
namespace ReadBadCode
{
publicpartial class frmTest :Form
{
BarCodeHook BarCode = newBarCodeHook();
public frmTest()
{
InitializeComponent();
BarCode.BarCodeEvent += newBarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);
}
private delegate void ShowInfoDelegate(BarCodeHook.BarCodesbarCode);
private void ShowInfo(BarCodeHook.BarCodesbarCode)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowInfoDelegate(ShowInfo), new object[] {barCode });
}
else
{
textBox1.Text =barCode.KeyName;
textBox2.Text =barCode.VirtKey.ToString();
textBox3.Text =barCode.ScanCode.ToString();
textBox4.Text =barCode.AscII.ToString();
textBox5.Text =barCode.Chr.ToString();
textBox6.Text = barCode.IsValid ? barCode.BarCode :"";
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ReadBadCode
{
publicpartial class frmTest : Form
{
BarCodeHook BarCode = new BarCodeHook();
public frmTest()
{
InitializeComponent();
BarCode.BarCodeEvent += newBarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);
}
private delegate void ShowInfoDelegate(BarCodeHook.BarCodesbarCode);
private void ShowInfo(BarCodeHook.BarCodes barCode)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowInfoDelegate(ShowInfo), new object[] {barCode });
}
else
{
textBox1.Text = barCode.KeyName;
textBox2.Text = barCode.VirtKey.ToString();
textBox3.Text = barCode.ScanCode.ToString();
textBox4.Text = barCode.AscII.ToString();
textBox5.Text = barCode.Chr.ToString();
textBox6.Text = barCode.IsValid ? barCode.BarCode : "";
}
}
void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
{
ShowInfo(barCode);
}
private void frmTest_Load(object sender, EventArgs e)
{
BarCode.Start();
}
private void frmTest_FormClosed(object sender, FormClosedEventArgse)
{
BarCode.Stop();
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
if (textBox6.Text.Length > 0)
{
MessageBox.Show(textBox6.Text);
}
}
}
}
BarCodeHook 類:
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
using System;
usingSystem.Collections.Generic;
using System.Text;
usingSystem.Runtime.InteropServices;
using System.Reflection;
namespace ReadBadCode
{
public classBarCodeHook
{
public delegate void BarCodeDelegate(BarCodesbarCode);
public event BarCodeDelegateBarCodeEvent;
public struct BarCodes
{
public intVirtKey; //虛擬碼
public intScanCode; //掃描碼
public string KeyName; //鍵名
public uintAscII; //AscII
public charChr; //字符
public string BarCode; //條碼資訊
public boolIsValid; //條碼是否有效
public DateTimeTime; //掃描時間
}
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, HookProclpfn, IntPtr hInstance, intthreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(intidHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode,Int32 wParam, IntPtrlParam);
[DllImport("user32", EntryPoint ="GetKeyNameText")]
private static extern int GetKeyNameText(int lParam, StringBuilderlpBuffer, 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[] lpKeyState, ref uint lpChar, intuFlags);
delegate int HookProc(int nCode, Int32 wParam, IntPtrlParam);
BarCodes barCode = newBarCodes();
int hKeyboardHook = 0;
string strBarCode = "";
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtrlParam)
{
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 = newStringBuilder(255);
if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255)> 0)
{
barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0'});
}
else
{
barCode.KeyName = "";
}
byte[] kbArray = newbyte[256];
uint uKey = 0;
GetKeyboardState(kbArray);
if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey,0))
{
barCode.AscII = uKey;
barCode.Chr =Convert.ToChar(uKey);
}
if (DateTime.Now.Subtract(barCode.Time).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, newHookProc(KeyboardHookProc),Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
}
return (hKeyboardHook !=0);
}
// 卸載鉤子
public bool Stop()
{
if (hKeyboardHook != 0)
{
returnUnhookWindowsHookEx(hKeyboardHook);
}
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
你這是C#代碼?發錯版塊了吧uj5u.com熱心網友回復:
你好!能否提供vb代碼,包括列印和掃描的源代碼,酬謝轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/59439.html
標籤:VBA
