在 Windows 启动时启动 C# 应用程序并保持焦点
我正在开发一个简单的程序,可以读取卡片或条形码并将其记录在数据库中。之前,我在 Visual Basic 中开发了相同的应用程序,它运行得很好,但由于我们的服务器发生了很多变化,我们决定用 C# 开发这个应用程序。
我可以通过 Windows 将其快捷方式放在开始菜单的“启动”上来启动我的应用程序,但问题是它没有获得焦点,因此无法读取卡片和条形码,这样我的程序就毫无用处了。我们使用的计算机运行的是 Windows XP 和 Windows 7。
在 Windows 启动中启动我的应用程序并保持焦点的最佳方式是什么?
I'm developing a simple program that reads a card or a barcode and logs that on a database. Before, I developed the same application in Visual Basic and it was working great, but due to a lot of changes on our servers, we decided to develop this app in C#.
I can get my application to start with Windows putting its shortcut on 'startup' of the start menu but the problem is that its not getting focus so that the cards and barcodes can be read and that way my program is simply useless. The machines we use are running Windows XP and Windows 7.
How is the best way to start my application on Windows Startup and keep the focus in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
总的来说,这是一个非常糟糕的设计。我刚刚完成了一个涉及条形码阅读器的项目,并将它们设置为串行端口,而不是键盘。您应该检查您的硬件是否有此选项,因为最终结果会更加可靠。
话虽这么说,您可以在表单中创建一个计时器,执行
this.Focus()
和this.BringToFront()
来窃取焦点。请注意,默认情况下,这只会导致任务栏闪烁。您需要使用 TweakUI 启用焦点窃取。此处讨论了另一种选择。实际上,非常相似的问题。基本上,您将键盘输入挂在较低的级别。
This is, in general, a pretty bad design. I just finished a project involving barcode readers and set them up to act as serial ports, instead of keyboards. You should check whether this is an option with your hardware, as the end result will be much more reliable.
That being said, you can create a timer in your form that executes
this.Focus()
andthis.BringToFront()
to steal focus. Be aware that this will, by default, only cause the task bar to flash. You'll need to use TweakUI to enable focus stealing.Another option is discussed here on SO. Very similar question, actually. Basically, you hook the keyboard input at a low level.
请尝试寻找其他方法,监视系统事件或使用轮询机制。
应避免窃取焦点,阅读本文以了解原因
如果我每次在错误的应用程序中输入密码、明文,因为它窃取了焦点,我就能得到一美元……
Please try and find another way, monitor system events or use a polling mechanism.
Stealing focus should be avoided, read this to understand why
If I had a dollar for every time I typed a password, cleartext in the wrong application because it stole focus...
在表单激活事件上调用 This.SentToBack()
On Form Activated Event call This.SentToBack()