带有c#windows窗体应用的条形码扫描仪 [英] Barcode Scanner with c# windows Form Application

查看:132
本文介绍了带有c#windows窗体应用的条形码扫描仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我的Android手机作为条形码扫描仪,



关于Android应用程序:

它适用于WiFi,PC和Android都是连接到相同的WiFi连接,并且android将扫描数据发送到PC。它适用于Windows记事本但不适用于c#windows表单应用程序。

是否有任何代码我必须编写或只是正确配置应用程序...?

解决方案

它适用于Windows记事本意味着你正在使用所谓的键盘楔形 http://en.wikipedia.org/wiki/Barcode_scanner#Keyboard_wedges [ ^ ]。



正如bowlturner正确指出的那样,你需要专注于对应用程序的某些控制。但是,这不是那么可靠,因为如果用户激活其主窗口,任何其他应用程序总是可以获得焦点。也许,键盘楔形设计的目的是为了简化开发,但事实上,只是产生了类似的问题。如果你真的想要制作一个可靠的解决方案,这一点都不简单:你需要在键盘事件上设置一个全局Windows挂钩并捕获扫描仪事件。请参阅:

http ://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx [ ^ ]。



由于这是本机Windows API,因此您需要将一些功能和类型导出到CLI。你可以使用P / Invoke来做到这一点:

http://en.wikipedia.org/wiki / P / Invoke [ ^ ],

http://msdn.microsoft.com/library/en -us / vcmxspec / html /vcmg_PlatformInvocationServices.asp [ ^ ]。



但是,这可能不是最佳方式。原因如下:首先,您需要一个全局,而不是本地钩子。根据Microsoft的说法,您无法在CLI(托管)应用程序中设置全局挂钩;相反,您需要为此目的创建一个本机DLL。您的应用程序将需要加载该DLL并组织与钩子的一些通信。所以,无论如何,C#还不够。我不知道您更喜欢什么样的原生平台语言,但我建议您使用C ++ / CLI,因为您可以立即达到所有目标:您可以创建一个本机DLL来设置钩子,您可以使用相同的项目如果您创建混合模式(托管+非托管)项目,则托管(CLI)代码。这样的项目将使你成为一个可以包含非托管代码的DLL,但是从.NET的角度来看,它可以作为常规.NET程序集的主要可执行模块,你可以在任何.NET程序集中引用它,例如,用C#编写。



C ++ / CLI在ECMA-372下标准化。请参阅:

http://en.wikipedia.org/wiki/C %2B%2B / CLI [ ^ ],

http://www.ecma-international .org / publications / standards / Ecma-372.htm [ ^ ],

http:// www.gotw.ca/publications/C++CLIRationale.pdf [ ^ ],

http:// msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ]。



Se e还: http://msdn.microsoft.com/en-us/magazine/cc188966.aspx [ ^ ]。



另请参阅我过去在相关主题上的答案:

键盘钩如何工作以及如何实现 [ ^ ],

如何在其他应用程序中设置窗口挂钩? [ ^ ],

创建全局快捷方式。 [ ^ ]。



-SA

作为谢尔盖答案的补充,你可以阅读一些很好的CodeProject文章。



.NET中的全局系统挂钩 [ ^ ]



使用窗口消息在C#中实现全局系统挂钩 [ ^ ]



这个我这不是一件小事,也有很多陷阱,例如,如果你有线程解决方案,你会遇到困难。



如果你对自己没有信心这种方法你总是可以使用 TextBox 并确保焦点在该控件上,但它基本上只在现实中有效,如果你只有一个窗口打开并连续返回焦点 TextBox

不是最好的解决方案,但可能会有效。


I use my Android phone as barcode scanner,

About Android App:
It works on WiFi, both PC and Android is connected to same WiFi connection and than android sends scanned data to PC. It works fine with windows notepad but not with c# windows form application.
Is there any code i have to write or just configure app correctly...?

解决方案

"It works fine with windows notepad" means that you are using so called "keyboard wedge": http://en.wikipedia.org/wiki/Barcode_scanner#Keyboard_wedges[^].

As bowlturner correctly pointed out, you need to keep focus on some control of your application. However, this is not so reliable way, because any other application can always grab focus if the user activates its main window. Probably, the purpose of keyboard wedge design was meant to be simplicity for development, but in fact, is just creates problems like that. An, if you really want to make a reliable solution, this is not simple at all: you will need to set a global Windows hook on keyboard events and capture the scanner events. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^].

As this is native Windows API, you will need to export some functions and types to CLI. You can do it using P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

However, this might be not the best way. Here is why: first of all, you will need a global, not a local hook. According to Microsoft, you cannot set a global hook in a CLI (managed) application; instead, you will need to create a native DLL for this purpose. Your application will need to load that DLL and organize some communication with the hook. So, C# won't be enough anyway. I don't know what native-platform language would you prefer, but I would advise using C++/CLI, because you can reach all goals at once: you can create a native DLL to set the hook, you can use the same project for managed (CLI) code if you create a mixed-mode (managed+unmanaged) project. Such project will make you a DLL which can contain unmanaged code, but, from the point of view of .NET, serve as main executable module of a regular .NET assembly which you can reference in any .NET assembly, for example, the one written in C#.

C++/CLI is standardized under ECMA-372. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

See also: http://msdn.microsoft.com/en-us/magazine/cc188966.aspx[^].

And see also my past answers on related topics:
How the Keyboard Hook works and how it is being implemented[^],
How to set a window hook in other application?[^],
Creating global shortcuts.[^].

—SA


As a complement to Sergey's answer there are some good CodeProject articles you can read.

Global System Hooks in .NET[^]

Using Window Messages to Implement Global System Hooks in C#[^]

This is not a trivial matter and there are lots of pitfalls, for example if you have a threaded solution you will run into difficulties.

If you don't feel confident about this approach you can always use a TextBox and make sure the focus is on that control, but it basically only works in reality if you only have one window open and continuously return focus to the TextBox.
Not the best of solutions, but it might work.


这篇关于带有c#windows窗体应用的条形码扫描仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆