Setting active a Chrome window (C++)

无人久伴 提交于 2019-12-11 02:19:47

问题


I am trying to set a Chrome Window to the foreground and active it in order to get the focus of the keyboard. My code works with Notepad or IE, but does not work with 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);

The code sets the Google Chrome window to the foreground but does not active it or focusing the keyboard on it. I don't know what is wrong. The results displayed are:

capture failed.
focus failed.
active failed.

What can I do?


回答1:


Well, I found the answer days ago.

There are two windows of Google Chrome with the same class name "Chrome_WidgetWin_1" and I was trying to active the first one, when the useful windows is the second one. So, I searched for the second window and later used SetForegroundWindow() with that window.

The result is:

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

Thanks anyways.



来源:https://stackoverflow.com/questions/21776450/setting-active-a-chrome-window-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!