C++ IUIAutomation获取微信消息

C++ IUIAutomation获取微信消息

前言

直接上代码

代码如下(示例):

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <atlbase.h>
#include <comutil.h>  
#include <UIAutomation.h>
#pragma comment(lib, "comsuppw.lib")
using namespace std;


HWND g_hwd=NULL;
int _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize(NULL);
	while(1)
	{
		g_hwd = FindWindow(_T("WeChatMainWndForPC"),_T("微信"));		//获取微信窗口的句柄
		if (g_hwd == NULL)
		{
			return 0;
		}
		else
		{
			WeChatInfo::UIAWeChat();
		}
		Sleep(1000);
	}
	CoUninitialize();

	return 0;
}

WeChatInfo::WeChatInfo()
{


}

WeChatInfo::~WeChatInfo()
{

}

SAFEARRAY* WeChatInfo::runtimeId = NULL;

void WeChatInfo::UIAWeChat()
{
	IUIAutomation *_automation;
	HRESULT hr = S_OK;
	if (FAILED(hr))
	{
		cout << "11" << endl;
		return;
	}
	else
	{
		hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&_automation));
		if (FAILED(hr))
		{
			cout << "22" << endl;
			return;
		}
		else
		{
			IUIAutomationElement *element = NULL;

			hr = _automation->ElementFromHandle(g_hwd, &element);
			if (FAILED(hr))
			{
				cout << "44" << endl;
				return;
			}

			if (SUCCEEDED(hr) && element != NULL)
			{
				IUIAutomationCondition* textPatternCondition;

				WCHAR wcPaneName[]=L"列表项目";  
				VARIANT trueVar;
				trueVar.vt=VT_BSTR;
				trueVar.bstrVal=SysAllocString(wcPaneName);

				//hr = _automation->CreateTrueCondition(&textPatternCondition);
				hr = _automation->CreatePropertyCondition(UIA_LocalizedControlTypePropertyId, trueVar, &textPatternCondition);  //设置条件接口
				if (FAILED(hr))
				{
					cout << "55" << endl;
					return;
				}
				else
				{
					IUIAutomationElementArray *textElement;
					hr = element->FindAll(TreeScope_Descendants, textPatternCondition, &textElement);
					if (FAILED(hr))
					{
						cout << "66" << endl;
						return;
					}
					else if (textElement == NULL)
					{
						cout << "77" << endl;
						return;
					}
					else
					{ 
						//获取控件内的文本
						int i_length;
						BSTR name;
						string plpszText;
						hr = textElement->get_Length(&i_length);
						if (FAILED(hr))
						{ 
							return;
						}
						
						IUIAutomationElement* pElement;
						hr = textElement->GetElement(i_length-1, &pElement);  //微信消息列表最上面一条消息
						if (FAILED(hr))
						{ 
							return;
						}

						WCHAR PaneName[]=L"按钮";  //消息对象名称
						VARIANT Var;
						Var.vt=VT_BSTR;
						Var.bstrVal=SysAllocString(PaneName);

						hr = _automation->CreatePropertyCondition(UIA_LocalizedControlTypePropertyId, Var, &textPatternCondition);
						if (FAILED(hr))
						{ 
							return;
						}
						IUIAutomationElementArray *ptextElement;
						pElement->FindAll(TreeScope_Subtree,textPatternCondition,&ptextElement);

						hr = ptextElement->get_Length(&i_length);
						if (FAILED(hr))
						{ 
							return;
						}

						IUIAutomationElement* p_Element;
						hr = ptextElement->GetElement(i_length-1, &p_Element);
						if (FAILED(hr))
						{ 
							return;
						}

						
						hr = p_Element->get_CurrentName(&name);
						if (FAILED(hr))
						{
							cout << "99" << endl;
							return;
						}

						plpszText = _com_util::ConvertBSTRToString(name);  
						SysFreeString(name); 

						if (FAILED(hr))
						{
							cout << "88" << endl;
							return;
						}
						

						
						hr = pElement->get_CurrentName(&name);
						if (FAILED(hr))
						{
							cout << "99" << endl;
							return;
						}

						plpszText += ":";
						plpszText +=  _com_util::ConvertBSTRToString(name);  
						SysFreeString(name); 

						SAFEARRAY *pruntimeId;
						BOOL trv;
						hr= pElement->GetRuntimeId(&pruntimeId);

						_automation->CompareRuntimeIds(pruntimeId,runtimeId,&trv);
						if(trv == TRUE)
						{
							return;
						}
						else
						{
							runtimeId = pruntimeId;
							printf("%s\n",plpszText.c_str());
						}
					}
					textPatternCondition->Release();
				}
			}
			_automation->Release();
		}
	}
	
	cout << "okok" << endl;
	return ;
}
  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在C#中,使用UI Automation库来选择c uiautomation controltype.listitem时,可以通过以下步骤进行选择: 1. 首先,需要获取应用程序的主窗口句柄。可以使用Win32 API函数来获取句柄。 2. 通过UI Automation库中的AutomationElement类的FindFirst方法,传入主窗口句柄和ControlType.ListItem参数来查找第一个List Item控件。 3. 如果需要获取所有的List Item控件,可以使用AutomationElement类的FindAll方法。 下面是一个示例代码片段,展示了如何使用UI Automation库选择c uiautomation controltype.listitem: ```csharp using System; using System.Windows.Automation; // 获取应用程序主窗口句柄 IntPtr mainWindowHandle = // 获取主窗口句柄的代码 // 使用UI Automation库找到第一个List Item控件 AutomationElement listItem = AutomationElement.FromHandle(mainWindowHandle).FindFirst( TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem)); // 使用UI Automation库找到所有的List Item控件 AutomationElementCollection listItemCollection = AutomationElement.FromHandle(mainWindowHandle).FindAll( TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem)); ``` 希望这个示例代码能够帮助你选择c uiautomation controltype.listitem。如果需要更多信息,可以参考引用中提供的文档和代码片段。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [自动操作软件 获取软件按钮内容 UIAutomation 软件自动化测试(我的一点补充)](https://blog.csdn.net/ddzr972435946/article/details/101154624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值