如何在Windows 8.1/10上可靠地窃取/重新获得MFC/桌面应用程序的焦点? [英] How to reliably steal/regain focus for MFC/desktop app on Windows 8.1/10?

查看:98
本文介绍了如何在Windows 8.1/10上可靠地窃取/重新获得MFC/桌面应用程序的焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我明白了:偷窃焦点是邪恶的.或者至少是99.9%的时间.但是我确实需要在Windows 8上可靠地窃取焦点,到目前为止,成群的人们坚持认为窃取焦点总是 邪恶.

OK, I get it: focus stealing is evil. Or at least it is 99.9% of the time. But I really need to steal the focus reliably on Windows 8, and so far I'm thwarted by the hordes of people insisting focus stealing is always evil.

场景:我们在运行Windows 8.1(即将成为Windows 10)的普通PC上运行自定义应用程序.屏幕,键盘和鼠标位于离地面约5m的地方,这是叉车操作员真正不应该爬的台阶.他们拥有的一个输入设备是数字键盘,其扩展电缆水平向下.他们需要执行的所有操作都可以从该键盘执行...只要某些恶意程序没有夺走我们应用程序的焦点,或者某个远程用户没有注销并让另一个具有焦点的应用程序.

Scenario: we run a custom application on an ordinary PC running Windows 8.1 (soon to be Windows 10). The screen, keyboard and mouse sit roughly 5m off the ground up some stairs that the forklift operator really shouldn't climb. The one input device they have is a numeric keypad on an extender cable down at their level. Everything they need to do they can do from that keypad... so long as some evil program hasn't stolen our application's focus, or some remote user hasn't logged out and left another application with focus.

该应用程序本质上是一个最大化的桌面应用程序-它充满了整个屏幕(但严格来说不是全屏"或最顶层"应用程序),因此允许其他应用程序在需要时显示在其前面.但是,当鼠标闲置时,我们希望该应用程序在所有其他应用程序之前恢复其正常"位置,以便它获得焦点并且数字小键盘输入将可靠地工作.

The application is essentially a maximised desktop application - it fills the screen (but is not strictly a "full screen" or "topmost" application), and therefore allows other applications to appear in front of it when required. But when the mouse goes idle, we want this application to resume its "normal" position in front of all other applications so that it gets focus and the numeric keypad input will work reliably.

在Windows 7上,使用SetForegroundWindow()(由AllowSetForegroundWindow()启用)可以很好地工作-可以将应用程序放回最前面并恢复焦点.在Windows 8上,SetForegroundWindow()仅导致任务栏图标闪烁,但是应用程序无法重新获得焦点,迫使我们的用户爬楼梯……在这里,完整的键盘和鼠标太诱人了,以至于他们不按不应按的按钮,并且通常会出现混乱.

On Windows 7, using SetForegroundWindow() (enabled by AllowSetForegroundWindow() works fine - the application can be brought back to the front and resume focus. On Windows 8, SetForegroundWindow() only results in the taskbar icon flashing, but the application does not regain focus, forcing our user to climb the stairs... where the full keyboard and mouse is too tempting for them not to press buttons they shouldn't, and chaos typically ensues.

因此,先生,请问:一旦鼠标闲置1分钟,我们的(MFC,桌面)应用程序是否可以窃取焦点,因为它或多或少是正常情况下仍应运行的唯一应用程序.如果允许的话,我们如何可靠地窃取它?

So please sir: can our (MFC, desktop) application steal back the focus once the mouse has gone idle for 1 minute, because it is more or less the only application that should normally be running anyway. If that is permitted, how do we steal it reliably?

推荐答案

在数字小键盘(RegisterHotKey)上配置热键.

Configure hotkeys on numeric keypad (RegisterHotKey).

按注册的热键会给您前台激活的热爱雷蒙·陈(Raymond Chen)

Pressing a registered hotkey gives you the foreground activation love by Raymond Chen

调用RegisterHotKey函数注册热键后, 当用户按下时,窗口管理器将向您发送WM_HOTKEY消息 该热键,以及它,您将获得前台的爱.如果 您从热键处理程序内部调用SetForegroundWindow, 前景窗口会根据您的指示进行更改.

After you call the RegisterHotKey function to register a hotkey, the window manager will send you a WM_HOTKEY message when the user presses that hotkey, and along with it, you will get the foreground love. If you call SetForegroundWindow from inside your hotkey handler, the foreground window will change according to your instructions.

这篇关于如何在Windows 8.1/10上可靠地窃取/重新获得MFC/桌面应用程序的焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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