1
我想設置一個Chrome窗口前臺並激活它,以獲得鍵盤的焦點。我的代碼適用於記事本或IE,但不適用於谷歌瀏覽器。設置活動的Chrome窗口(C++)
//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.
我該怎麼辦?