在 Windows 启动时启动 C# 应用程序并保持焦点

发布于 2024-11-26 14:21:47 字数 283 浏览 0 评论 0原文

我正在开发一个简单的程序,可以读取卡片或条形码并将其记录在数据库中。之前,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世俗缘 2024-12-03 14:21:47

总的来说,这是一个非常糟糕的设计。我刚刚完成了一个涉及条形码阅读器的项目,并将它们设置为串行端口,而不是键盘。您应该检查您的硬件是否有此选项,因为最终结果会更加可靠。

话虽这么说,您可以在表单中创建一个计时器,执行 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() and this.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.

黄昏下泛黄的笔记 2024-12-03 14:21:47

请尝试寻找其他方法,监视系统事件或使用轮询机制。

应避免窃取焦点,阅读本文以了解原因

如果我每次在错误的应用程序中输入密码、明文,因为它窃取了焦点,我就能得到一美元……

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...

晨光如昨 2024-12-03 14:21:47

在表单激活事件上调用 This.SentToBack()

On Form Activated Event call This.SentToBack()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文