我用的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();