您的位置:首页 > 其它

MFC ActiveX访问WebBrowser(读取本地登录的QQ信息)

2013-10-30 16:10 323 查看
我用的MFC编写的ActiveX。

要点1:项目中引入WebBrowserActiveX控件



要点2:由于WebBrowser本身是一个ActiveX组件,请在“项目名称App"类的initInstance()方法中添加:AfxEnableControlContainer();

要点3:添加全局变量,并在Create方法中创建ActiveX,并调用navigate方法:









要点4:通过谷歌浏览器分析浏览器读取到的本地QQ号的网页内容格式:



要点5:添加ActiveX组件对外暴露的接口方法,并代码实现:



fetchList()方法的代码:

CString strResult;
// TODO: Add your dispatch handler code here
if(m_wb.GetReadyState()!=READYSTATE_COMPLETE)
{
//	AfxMessageBox("尚未加载完成");
return strResult.AllocSysString();
}

USES_CONVERSION;

// Get the WebBrowser's document object
CComPtr<IDispatch> pDispDocument= m_wb.GetDocument();

HRESULT hr = NULL;

CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pHtmlDocument = pDispDocument;
IHTMLElement * m_pBody=NULL;
// Extract the source code of the document
if (pHtmlDocument)
{
// Get the BODY object
hr = pHtmlDocument->get_body(&m_pBody);
if (FAILED(hr))
return strResult.AllocSysString();

CComQIPtr< IHTMLElementCollection > spElementCollection;//元素集合
CComPtr< IDispatch > spDisp;
CComQIPtr < IHTMLElement > spDiv;
pHtmlDocument->get_all(&spElementCollection);//获得元素的集合
if(spElementCollection)
{
//从元素集合中查找id为"list_uin"的元素
spElementCollection->item(CComVariant(CComBSTR("list_uin")),CComVariant(),&spDisp);
spDiv = spDisp;

//获取这个div内的文本确定是否有QQ登录
BSTR divbstrHTMLText;
spDiv->get_innerHTML(&divbstrHTMLText);//获取div类的文本
if(SysStringLen(divbstrHTMLText)==0)//如果长度为零说明没有登录
{
//AfxMessageBox("没有登录的QQ");
strResult.Insert(0,"没有登录的QQ");
return strResult.AllocSysString();
}

//程序周到这里说明已经登录

CComPtr<IDispatch> pDispLi;
CComQIPtr<IHTMLElementCollection> pElementColletionsLi;
spDiv->get_children(&pDispLi);//获取div的所有子元素li,每个li是一个登录的QQ
pElementColletionsLi = pDispLi;

CComPtr< IDispatch > pDispLi2;
CComQIPtr < IHTMLElementCollection > pElementColletionsLi2;
pElementColletionsLi->tags(CComVariant(CComBSTR("li")),&pDispLi2);
pElementColletionsLi2 = pDispLi2;

long licount=0;
pElementColletionsLi2->get_length(&licount);
for (int i=0;i<licount;i++)
{
CComPtr< IDispatch > pDispLi3;
CComQIPtr < IHTMLElement > pElementLi;
pElementColletionsLi2->item(CComVariant(i),CComVariant(i),&pDispLi3);//获得当前这个li
pElementLi = pDispLi3;

//以下准备获取li里面的label
CComPtr<IDispatch> pDispLabel;
CComQIPtr<IHTMLElementCollection> pElementColletionsLabel;
CComPtr<IDispatch> pDispLabel2;
CComQIPtr<IHTMLElementCollection> pElementColletionsLabel2;

CComPtr< IDispatch > mylablespDisp;
CComQIPtr < IHTMLElementCollection > spLabel;
CComPtr< IDispatch > myLabelspDisp;

pElementLi->get_children(&pDispLabel);
pElementColletionsLabel = pDispLabel;
pElementColletionsLabel->tags(CComVariant(CComBSTR("label")),&pDispLabel2);
pElementColletionsLabel2 = pDispLabel2;

long labelCount = 0;
pElementColletionsLabel2->get_length(&labelCount);
for (int i=0;i<labelCount;i++)
{
CComPtr<IDispatch> pDispLabel3;
CComQIPtr < IHTMLElement > pLabel;
BSTR myLabelInnerHTML;

pElementColletionsLabel2->item(CComVariant(0),CComVariant(0),&pDispLabel3);
pLabel = pDispLabel3;
pLabel->get_innerHTML(&myLabelInnerHTML);
CString line(myLabelInnerHTML);
CString niceName = line;
CString qq = line;
niceName = niceName.Left(niceName.Find(" "));
qq.Delete(0,qq.Find("(")+1);
qq.Delete(qq.GetLength()-1,1);
strResult +=","+niceName+":"+qq;
}
}
}
}
else
{
strResult.Insert(0,"这不是一个有效的html");
}
if(strResult.GetAt(0)==',')
{
strResult.Delete(0,1);
}
return strResult.AllocSysString();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: