我有一个 dll,当用户按下网页上的按钮时,浏览器插件会调用该 dll。这个 dll 包含创建一个窗口(在新的 UI 线程中)的 native 代码,我需要这个窗口来获得键盘焦点来创建它(用户期望的行为)。我并不是想把不需要的弹出窗口推到用户面前。
我尝试过使用SetForegroundWindow()、SetFocus()、BringWindowToTop()、AttachThreadInput(),最小化/恢复窗口,只要你能想到的,我都试过了。我取得的最大进展是让新窗口在任务栏中闪烁。
我需要能够执行此操作的原因是因为我需要考虑键盘可访问性,并允许用户使用我的新窗口而无需使用 alt-tab 切换到它。
有人对如何在此处获得键盘焦点有任何建议吗?
最佳答案
如果窗口在任务栏上闪烁,则您的代码不符合 SetForegroundWindow()
documentation 中概述的要求:
A process can set the foreground window only if one of the following conditions is true:
The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see LockSetForegroundWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
No menus are active.
An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.
关于c++ - WinApi:将键盘焦点赋予新线程中的新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475717/