保持窗口顶部和窃取焦点中的WinForms [英] Keep window on top and steal focus in WinForms

查看:165
本文介绍了保持窗口顶部和窃取焦点中的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这将是完全不好的做法,在正常情况下,但是这仅仅是一个测试应用程序,需要从酒吧code扫描仪获取输入(模拟键盘)。问题是,我要开始了一些脚本,而扫描的,所以我需要在窗口直接重新获得焦点后,我点击脚本来运行它。我已经使用激活(),BringToFront(),焦点()以及一些Win32调用像SetForegroundWindow(),Setcapture()和setActiveWindow()就...但最好的,我可以得到任何他们做的是试图使任务栏项开始闪烁告诉我,它的希望的具有焦点,但事情是阻止它。顺便说一句,我在XP SP2上运行这一点,并使用.NET 2.0。

I realize that this would be COMPLETELY bad practice in normal situations, but this is just for a test app that needs to be taking input from a bar code scanner (emulating a keyboard). The problem is that I need to start up some scripts while scanning, so I need the window to regain focus directly after I click the script to run it. I've tried using Activate(), BringToFront(), Focus() as well as some Win32 calls like SetForegroundWindow(), Setcapture() and SetActiveWindow()... however the best I can get any of them to do is to make the taskbar item start blinking to tell me that it wants to have focus, but something is stopping it. BTW, I'm running this on XP SP2 and using .NET 2.0.

这可能吗?

编辑:为了澄清,我在浏览器上运行的脚本双击。所以,我需要它来窃取焦点回来从资源管理器和测试应用程序。

To clarify, I am running the scripts by double-clicking on them in explorer. So I need it to steal focus back from explorer and to the test app.

推荐答案

请在窗口中最顶层窗口。这是方式的任务管理器可以保持在其他窗口的顶部。这是一个表格的属性,你使窗体最顶部的值设置为 真(浮在上面的其他窗口)

Visibility

Make the window a "Top-Most" window. This is the way the Task-Manager can remain on top of other windows. This is a property of a Form and you make the form top-most (floating above other windows) by setting the value to true.

您应该不需要重写任何与最上面的设置活动窗口的行为。

You shouldn't need to override any of the "Active window" behaviour with the top-most setting.

我问过类似的问题 previously这里计算器并答案将解决您的问题。您可以在应用程序中使用一个低级别的投入挂钩,并获得键盘codeS扫描仪即将到来的通知。这样一来,你的应用程序始终得到这些按键即使应用程序没有焦点。

I asked a similar question previously here on StackOverflow and the answer would solve your problem. You can make the application use a low-level input hook and get notification of the key-codes coming from the scanner. This way, your application always gets these keys even though the application does not have focus.

您可能需要增强解决方案壁球​​键盘codeS,使他们不被发送到焦点对准应用程序(如记事本)。

You may need to enhance the solution to squash the key-codes so that they are not transmitted to the "in-focus" application (e.g. notepad).

由于Windows 2000,有一个应用程序,以抓住重点未经用户直接干预没有正式的机制。偷看的输入流通过RawInputDevices挂钩是唯一明智的路要走。

Since Windows 2000, there is no official mechanism for an application to grab focus without direct intervention of the user. Peeking at the input streams through the RawInputDevices hook is the only sensible way to go.

若干文章可以帮助(C#实现)

A number of articles may help (C# implementations)

  • RawInput article on CodeProject
  • MSDN documentation of RawInput

这篇关于保持窗口顶部和窃取焦点中的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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