Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top