使用C ++在Chrome中获取有效的标签网址 [英] Get active Tab URL in Chrome with C++
问题描述
名称:地址和搜索栏*
ControlType:UIA_EditControlTypeId (0xC354)
要在Chrome中找到活动选项卡,请使用 FindWindowEx
在桌面上查找第一个可见的Chrome子窗口。
以下示例使用ATL COM类,它需要Visual Studio
#define UNICODE
#include< Windows.h>
#include< AtlBase.h>
#include< AtlCom.h>
#include< UIAutomation.h>
int main()
{
CoInitialize(NULL);
HWND hwnd = NULL;
while(true)
{
hwnd = FindWindowEx(0,hwnd,LChrome_WidgetWin_1,NULL);
if(!hwnd)
break;
if(!IsWindowVisible(hwnd))
continue;
CComQIPtr< IUIAutomation> UIA;
if(FAILED(uia.CoCreateInstance(CLSID_CUIAutomation))||!uia)
break;
CComPtr< IUIAutomationElement>根;
if(FAILED(uia-> ElementFromHandle(hwnd,& root))||!root)
break;
CComPtr< IUIAutomationCondition>条件;
// URL的id是0xC354,或者对第一个编辑框使用UIA_EditControlTypeId
uia-> CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(0xC354),& condition);
//或者使用编辑控件的名称来代替
// uia-> CreatePropertyCondition(UIA_NamePropertyId,
// CComVariant(LAddress and search bar),& condition );
CComPtr< IUIAutomationElement>编辑;
if(FAILED(root-> FindFirst(TreeScope_Descendants,condition,& edit))
||!edit)
continue; //也许我们没有正确的选项卡,请继续...
CComVariant url;
edit-> GetCurrentPropertyValue(UIA_ValueValuePropertyId,& url);
MessageBox(0,url.bstrVal,0,0);
休息;
}
CoUninitialize();
返回0;
对于非英语系统,可能会有地址和搜索栏
There are several answered questions about this on stackoverflow, but they seem to be outdated and don't work anymore. Chrome has changed its structure entirely. If I try the AccessibleObjectFromEvent technique, then I just get NULL values for the accName and accValue. It seems that there are solutions for python, however I could not find any solution for C++. How can I retrieve the active Tab URL in C++?
Using the Inspect tool in Window SDK, we can get the property name for Chrome's URL edit box:
Name: "Address and search bar" *
ControlType: UIA_EditControlTypeId (0xC354)
To find the active tab in Chrome, use FindWindowEx
to find the first visible Chrome child window in desktop.
Then use UI Automation to find the edit control with that id. Or just find the first edit control in Chrome.
The following example uses ATL COM classes, it requires Visual Studio
#define UNICODE
#include <Windows.h>
#include <AtlBase.h>
#include <AtlCom.h>
#include <UIAutomation.h>
int main()
{
CoInitialize(NULL);
HWND hwnd = NULL;
while(true)
{
hwnd = FindWindowEx(0, hwnd, L"Chrome_WidgetWin_1", NULL);
if(!hwnd)
break;
if(!IsWindowVisible(hwnd))
continue;
CComQIPtr<IUIAutomation> uia;
if(FAILED(uia.CoCreateInstance(CLSID_CUIAutomation)) || !uia)
break;
CComPtr<IUIAutomationElement> root;
if(FAILED(uia->ElementFromHandle(hwnd, &root)) || !root)
break;
CComPtr<IUIAutomationCondition> condition;
//URL's id is 0xC354, or use UIA_EditControlTypeId for 1st edit box
uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(0xC354), &condition);
//or use edit control's name instead
//uia->CreatePropertyCondition(UIA_NamePropertyId,
// CComVariant(L"Address and search bar"), &condition);
CComPtr<IUIAutomationElement> edit;
if(FAILED(root->FindFirst(TreeScope_Descendants, condition, &edit))
|| !edit)
continue; //maybe we don't have the right tab, continue...
CComVariant url;
edit->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &url);
MessageBox(0, url.bstrVal, 0, 0);
break;
}
CoUninitialize();
return 0;
}
For non-English system there could be a different name for "Address and search bar"
这篇关于使用C ++在Chrome中获取有效的标签网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!