知识大全 C#操控条形码扫描枪
Posted 条形码
篇首语:人生难得几回搏,此时不搏待何时。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#操控条形码扫描枪相关的知识,希望对你有一定的参考价值。
C#操控条形码扫描枪 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
// 条码扫描器
// 窗体部分相关代码
using System;
using System Collections Generic;
using System ComponentModel;
using System Data;
using System Drawing;
using System Text;
using System Windows Forms;
namespace ReadBadCode
public partial class frmTest : Form
BarCodeHook BarCode = new BarCodeHook();
public frmTest()
InitializeComponent();
BarCode BarCodeEvent += new BarCodeHook BarCodeDelegate(BarCode_BarCodeEvent);
private delegate void ShowInfoDelegate(BarCodeHook BarCodes barCode);
private void ShowInfo(BarCodeHook BarCodes barCode)
if (this InvokeRequired)
this BeginInvoke(new ShowInfoDelegate(ShowInfo) new object[] barCode );
else
textBox Text = barCode KeyName;
textBox Text = barCode VirtKey ToString();
textBox Text = barCode ScanCode ToString();
textBox Text = barCode AscII ToString();
textBox Text = barCode Chr ToString();
textBox 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 FormClosedEventArgs e)
BarCode Stop();
private void textBox _TextChanged(object sender EventArgs e)
if (textBox Text Length > )
MessageBox Show(textBox Text);
BarCodeHook 类相关代码
using System;
using System Collections Generic;
using System Text;
using System Runtime InteropServices;
using System Reflection;
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( user dll CharSet = CharSet Auto CallingConvention = CallingConvention StdCall)]
private static extern int SetWindowsHookEx(int idHook HookProc lpfn IntPtr hInstance int threadId);
[DllImport( user dll CharSet = CharSet Auto CallingConvention = CallingConvention StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);
[DllImport( user dll CharSet = CharSet Auto CallingConvention = CallingConvention StdCall)]
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)Marshal PtrToStructure(lParam typeof(EventMsg));
if (wParam == x ) //WM_KEYDOWN = x
barCode VirtKey = ssage & xff; //虚拟码
barCode ScanCode = msg paramL & xff; //扫描码
StringBuilder strKeyName = new StringBuilder( );
if (GetKeyNameText(barCode ScanCode * strKeyName ) > )
barCode KeyName = strKeyName ToString() Trim(new char[] \\ );
else
barCode KeyName = ;
byte[] kbArray = new byte[ ];
uint uKey = ;
GetKeyboardState(kbArray);
if (ToAscii(barCode VirtKey barCode ScanCode kbArray ref uKey ))
barCode AscII = uKey;
barCode Chr = Convert ToChar(uKey);
if (DateTime Now Subtract(barCode Time) TotalMilliseconds > )
strBarCode = barCode Chr ToString();
else
if ((ssage & xff) == && strBarCode Length > ) //回车
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 == )
//WH_KEYBOARD_LL =
hKeyboardHook = SetWindowsHookEx( new HookProc(KeyboardHookProc) Marshal GetHINSTANCE(Assembly GetExecutingAssembly() GetModules()[ ]) );
return (hKeyboardHook != );
// 卸载钩子
public bool Stop()
if (hKeyboardHook != )
return UnhookWindowsHookEx(hKeyboardHook);
return true;
cha138/Article/program/net/201311/12265
相关参考
手机条码扫描不灵啊?对电脑屏幕上的方的那个二维码管用,但是对商品上的条形码根本不管用管用,手机识别条形码是分码制的,开启一个相关的程序只能扫一种码。39码要有39码程序,128码要有128码程序,en
我想买一台超市里的收款机,好像还要用扫描枪扫描,不知道怎么用,怎么把价格弄到物品上的条形码去!具体第一个肯定是要把价格输到电脑里的,不然他怎么知道你卖的东西价格呢。东西实施起来非常方便。先需要确定两点
超市的条形码防盗是什么原理??(答对追加50分)条形码的识别原理由于不同颜色的物体,其反射的可见光的波长不同,白色物体能反射各种波长的可见光,黑色物体则吸收各种波长的可见光,所以当条形码扫描器光源发出
知识大全 据说是日本的化妆品但是在中国的网站能扫出条形码是怎么回事
据说是日本的化妆品但是在中国的网站能扫出条形码是怎么回事我用微信扫描出来的信息不尽相同,结果是没有任何可参考性。我在海口机场免税店买的三个产品扫码两个没有产品信息一个显示来自法国,我在泰国kingpo
怎样用二维码来辨别商品的真假手机软件扫码结果只能当作参考,并不能用来直接断定商品的真假。因为商品的条码是可以被复制,如果在假冒伪劣产品上喷涂正规商品的条形码或二维码,那么扫描出来的结果也是不准确的。二
一个人在站立时不应操控多重的对象,否则会增加背部受伤的机会?
一个人在站立时不应操控多重的对象,否则会增加背部受伤的机会?。A.12千克B.14千克C.16千克【参考答案】C[拓展知识]如提举或放下负荷物的距离超过多少,会增加受伤的机会?A.15厘米B.20厘米
一个人在站立时不应操控多重的对象,否则会增加背部受伤的机会?
一个人在站立时不应操控多重的对象,否则会增加背部受伤的机会?。A.12千克B.14千克C.16千克【参考答案】C[拓展知识]如提举或放下负荷物的距离超过多少,会增加受伤的机会?A.15厘米B.20厘米
超市的条形码是怎么做到防盗的?1、条形码如果有磁性,是生产厂商加上去的,还是超市加的?--------一般商品本身自带条形码,有些商品是超市打印贴上去的,条形码本身没有磁性,是加上去的。2、如果是超市
如何查询外国商品的条形码?或者帮我查询一下?求高人指点~~~~~5000378999703,5051594000001NutriciaLtdT/ADanoneBabyNutritDanoneBabyN
知识大全 美国条形码查询858752003028 帮我查查这个的资料 谢谢
美国条形码查询858752003028帮我查查这个的资料谢谢UnitedPharmaLLCCA2317MooreAve92833FullertonUnitedStates美国条形码查询75127385