CSDN博客

img skyremember

玩 IHTMLElementCollection

发表于2008/9/28 13:13:00  2214人阅读

void CGetLinksDlg::OnBnClickedGetlinks()
{
TCHAR HostName[2*MAX_PATH];
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLAnchorElement> pLoct;
// TODO: Add your control notification handler code here
int n = m_LinksList.GetItemCount();//GetCount();

for (int i = 0; i < n; i ++){
IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_LinksList.GetItemData(i);
if (pBrowser){
pBrowser->Release();
}
}

m_LinksList.DeleteAllItems();
m_LinksNum = 0;
Log("**************************************************************");
Log("/r/n");

if (m_spSHWinds){
int n = m_spSHWinds->GetCount();
for (int i = 0; i < n; i++){
_variant_t v = (long)i;
IDispatchPtr spDisp = m_spSHWinds->Item(v);


SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一个IE窗口的智能指针
if (spBrowser){

if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL)
{
if (SUCCEEDED(pDoc2->get_links(&pElementCol)))
{
// AfxMessageBox("IHTMLElementCollection");
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)))
if(p!=0)
{   
m_LinksNum = m_LinksNum+p;
UpdateData(FALSE);

for(long i=0;i<=(p-1);i++)
{

CComBSTR String;
_variant_t index = i;
if(SUCCEEDED(pElementCol->item( index, index, &spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface( IID_IHTMLAnchorElement,(void **) &pLoct)))

pLoct->get_href(&String);
String="http://www.baidu.com";
pLoct->put_href(String);
//pLoct->get_name(&String);

ZeroMemory(HostName,2*MAX_PATH);
lstrcpy(HostName,_bstr_t(String)); 
//添加到列表
m_LinksList.InsertItem(i,HostName);
m_LinksList.SetCheck(i,TRUE);
                                   
pLoct->get_hostname(&String);
                                    ZeroMemory(HostName,2*MAX_PATH);
lstrcpy(HostName,_bstr_t(String)); 
if(lstrlen(HostName))
{
m_LinksList.SetItemText(i,1,HostName);
Log(HostName );
Log("/r/n");
}
}
}
}

}

}
}
}
}
阅读全文
0 0

相关文章推荐

img
取 消
img