设置激活 Chrome 窗口 (C++)
Setting active a Chrome window (C++)
我正在尝试将Chrome窗口设置为前台并将其激活,以便获得键盘的焦点。我的代码适用于记事本或IE,但不适用于谷歌浏览器。
//Getting the HWND of Chrome
HWND chromeWindow = FindWindow("Chrome_WidgetWin_1", NULL);
DWORD dwCurrentThread = GetCurrentThreadId();
DWORD dwFGThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
AttachThreadInput(dwCurrentThread, dwFGThread, TRUE);
//Actions
AllowSetForegroundWindow(ASFW_ANY);
bool fore =SetForegroundWindow(chromeWindow);
if(fore==false){cout << "fore failed"<<endl;}
bool capture = SetCapture(chromeWindow);
if(capture==false){cout << "capture failed" <<endl;}
bool focus = SetFocus(chromeWindow);
if(focus==false){cout << "focus failed"<<endl;}
bool active = SetActiveWindow(chromeWindow);
if(active==false){cout << "active failed"<<endl;}
//Finishing
AttachThreadInput(dwCurrentThread, dwFGThread, FALSE);
该代码将谷歌浏览器窗口设置为前台,但不会激活它或将键盘聚焦在其上。我不知道出了什么问题。显示的结果是:
capture failed.
focus failed.
active failed.
我能做什么?
好吧,我几天前就找到了答案。
谷歌浏览器有两个具有相同类名"Chrome_WidgetWin_1"的窗口,我试图激活第一个,而有用的窗口是第二个。因此,我搜索了第二个窗口,后来在该窗口中使用了SetForegroundWindow()。
结果是:
//Getting the HWND of Chrome
HWND chromeWindow = FindWindow("Chrome_WidgetWin_1", NULL);
HWND chrome = GetWindow(chromeWindow, GW_HWNDNEXT);
//Setting the window to the foreground (implies focus and activating)
SetForegroundWindow(chrome);
无论如何,谢谢。
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- SFML RenderWindow打开窗口需要很长时间
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 获取 SFML 窗口的 HWND 和高可用性?
- 如何获取 GLFW 窗口 ID?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 用于窗口的 HID 终端
- SFML 文本未绘制在窗口上
- 如何为窗口截屏
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息