c++ - WinApi:将键盘焦点赋予新线程中的新窗口

标签 c++ winapi keyboard focus accessibility

我有一个 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/

相关文章:

Bash 脚本箭头键产生奇怪的字符

c# - 您会使用哪种 C# 项目类型重新开发 MFC C++ activex 控件?

c - FindFirstFile 和 Junctions

c# - Monitor类是如何实现的?

c++ - 在 c++ win32 中绘制/打印具有透明背景的文本

ios - 以编程方式禁用键盘声音

Android webview 软键盘激活时向下滚动

c++ - 使用 C++ 拆分 "[General Setting]"格式的节字符串

C++ builder 滚动框组件响应鼠标滚轮

c++ - 内联和非内联