设置活动的Chrome窗口(C ++) [英] Setting active a Chrome window (C++)

查看:873
本文介绍了设置活动的Chrome窗口(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Chrome窗口设置为前景并激活它,以获得键盘的焦点。

  //获取Chrome的HWND 
HWND chromeWindow = FindWindow(Chrome_WidgetWin_1,NULL);

DWORD dwCurrentThread = GetCurrentThreadId();
DWORD dwFGThread = GetWindowThreadProcessId(GetForegroundWindow(),NULL);
AttachThreadInput(dwCurrentThread,dwFGThread,TRUE);

//操作
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;}

//完成
AttachThreadInput(dwCurrentThread,dwFGThread,FALSE);

此代码将Google Chrome浏览器窗口设置为前台,但未激活或将键盘聚焦。我不知道是什么问题。显示的结果是:

 捕获失败。 
焦点失败。
活动失败。

我能做什么?



谷歌Chrome浏览器有两个窗口,类名为Chrome_WidgetWin_1和I试图激活第一个,当有用的窗口是第二个。所以,我搜索第二个窗口,后来用该窗口使用SetForegroundWindow()。



结果是:

  //获取Chrome的HWND 
HWND chromeWindow = FindWindow(Chrome_WidgetWin_1,NULL);
HWND chrome = GetWindow(chromeWindow,GW_HWNDNEXT);

//将窗口设置为前景(意味着焦点和激活)
SetForegroundWindow(chrome);

非常感谢。


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?

解决方案

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.

这篇关于设置活动的Chrome窗口(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆