c# - 设置事件 Chrome 窗口 (C++)

标签 c# c++ google-chrome hwnd

我正在尝试将 Chrome 窗口设置为前台并激活它以获得键盘的焦点。我的代码适用于记事本或 IE,但不适用于 Google Chrome。

//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);

该代码将 Google Chrome 窗口设置为前台,但不会激活它或将键盘聚焦在其上。我不知道出了什么问题。显示的结果是:

capture failed.
focus failed.
active failed.

我能做什么?

最佳答案

嗯,我几天前就找到了答案。

Google Chrome 有两个具有相同类名“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);

无论如何,谢谢。

关于c# - 设置事件 Chrome 窗口 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21776450/

相关文章:

c# - 将 NuGet 包发布到 Github

c# - DllImport 用于一组导入函数

c# - 如何让我的程序生成一个新的随机数,直到它找到一个以前没有使用过的随机数?

c++ - 对象具有与成员函数不兼容的类型限定符

c++ - 在 C++ 11 中使用 Homebrew 软件、gcc 和 llvm

javascript - 将图像 URL 发送到特定文本字段的 Google Chrome 扩展程序

c# - Guid.Parse 语句中未处理异常

c++ - 加载飞 map 像 "A device attached to the system is not functioning"

java - 无法使用 Selenium 中的用户配置文件启动 Chrome 浏览器

javascript - 仅执行脚本一次,直到下一次刷新