条码扫描器接口编程C

上传人:鲁** 文档编号:560025376 上传时间:2023-10-31 格式:DOC 页数:6 大小:73.50KB
返回 下载 相关 举报
条码扫描器接口编程C_第1页
第1页 / 共6页
条码扫描器接口编程C_第2页
第2页 / 共6页
条码扫描器接口编程C_第3页
第3页 / 共6页
条码扫描器接口编程C_第4页
第4页 / 共6页
条码扫描器接口编程C_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《条码扫描器接口编程C》由会员分享,可在线阅读,更多相关《条码扫描器接口编程C(6页珍藏版)》请在金锄头文库上搜索。

1、条码扫描器接口编程(C#)3年前写了一篇USB口的红外条形码扫描器的另类使用,不过相关代码是VB编写,在这几年之间,有许多网友提出需要C#版的,起初还以为由VB修改C#应该很容易,最近研究了一下,发现C#和VB调用API的机制还是有所不同的,在迁移的过程中还是会遇到不少问题,所以我专门抽时间做了一个基于C#的程序。 【目前的条形码扫描器有点类似外接键盘(其实从消息传送上它就相当于一个键盘),把输入焦点定位到可输入的控件上,一扫描相应的条形码信息就输入到文本框中去了,但是如果没有输入焦点,或另一个不相干的程序获得输入焦点,那就有点乱套了。我想实现的是,不管什么情况,只要扫描器一工作,我的程序就能

2、自动激活,并能获得当前输入的条形码信息。 实现思路:我用的是litele牌的USB口的红外条形码扫描器,仔细分析了一下,扫描成功后,以键盘按键消息的形式把条形码输入信息通知给系统。这样通过键盘钩子就可以方便的获得该信息了。但是,怎样区分信息是键盘还是条形码输入的哪?很简单,条形码扫描器在很短的时间内输入了至少3个字符以上信息,并且以“回车”作为结束字符,在这种思想指引下,很完美的实现了预定功能。】 窗体相关代码:view plaincopy to clipboardprint?102030405060708090100110120130140150using System;using Syst

3、em.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 += ne

4、w 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 textBox1.Text = barCode.

5、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(b

6、arCode); private void frmTest_Load(object sender, EventArgs e) BarCode.Start(); private void frmTest_FormClosed(object sender, FormClosedEventArgs e) BarCode.Stop(); private void textBox6_TextChanged(object sender, EventArgs e) if (textBox6.Text.Length 0) MessageBox.Show(textBox6.Text); BarCodeHook

7、类:view plaincopy to clipboardprint?102030405060708090100110120130140150using 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); p

8、ublic 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

9、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(us

10、er32.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, In

11、t32 wParam, IntPtr lParam); DllImport(user32, EntryPoint = GetKeyNameText) private static extern int GetKeyNameText(int lParam, 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 lpKeyState, ref uint lpChar, int uFlags);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号