标签:c google-chrome winapi
有several answered questions about this on stackoverflow,但它们似乎已经过时,不再工作了. Chrome有changed its structure entirely.如果我尝试AccessibleObjectFromEvent技术,那么我只获得accName和accValue的NULL值.看来there are solutions for python,但是我找不到任何解决方案.如何在C中检索活动的Tab URL?
解决方法:
使用Window SDK中的Inspect工具,我们可以获取Chrome的URL编辑框的属性名称:
Name: "Address and search bar" *
ControlType: UIA_EditControlTypeId (0xC354)
要在Chrome中查找有效标签,请使用FindWindowEx查找桌面中第一个可见的Chrome子窗口.
然后使用UI Automation查找具有该ID的编辑控件.或者只是在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, 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;
}
对于非英语系统,“地址和搜索栏”可能有不同的名称
标签:c,google-chrome,winapi 来源: https://codeday.me/bug/20191007/1864430.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。