本文主要是介绍在HtmlView中如何改变链接.(在线守候),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在HtmlView中如何改变链接.(在线守候)
在HtmlView中如何改变链接.
如在网页中有二个链接
<a href="aa.htm">aa</a>
<a href="bb.htm">bb</a>
现在我想改变<a href="aa.htm">aa</a>的链接.改成<a href="cc.htm">cc</a>.
请问有什么方法呢?千万别告诉我用查找字符串替换字符串哦.
问题点数:100、回复次数:5Top
1 楼masterz(www.fruitfruit.com)回复于 2002-06-12 10:32:32 得分 0
///
//change_anchor_href
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///
void change_anchor_href()
{
//CComQIPtr<IWebBrowser2> m_spBrowser;
HRESULT hr;
if (m_spBrowser != NULL)
{
IDispatchPtr spDisp=NULL;
hr=m_spBrowser->get_Document(&spDisp);
if(SUCCEEDED(hr)&& spDisp!= NULL )
{
IHTMLDocument2Ptr spHtmlDocument(spDisp);
IHTMLElementPtr spHtmlElement;
spHtmlDocument->get_body(&spHtmlElement);
IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
IHTMLElement* pElem=NULL;
_variant_t index;
index.vt=VT_I4;
IDispatchPtr disp;
long num;
hr=pColl->get_length(&num);
if(SUCCEEDED(hr))
{
for(long i=0;i<num;i++)
{
index.intVal=i;
hr=pColl->item(index,index,&disp);
IHTMLAnchorElementPtr pAnchorElement;
if(SUCCEEDED(hr)&&disp!=NULL)
{
hr=disp->QueryInterface(&pAnchorElement);
}
if (SUCCEEDED(hr)&& pAnchorElement != NULL)
{
pAnchorElement->put_href(_bstr_t("c://tmp//link.htm"));
}
}
}
pColl->Release();
}
}
}
}Top
2 楼masterz(www.fruitfruit.com)回复于 2002-06-12 10:36:19 得分 0
you can use CHtmlView::GetHtmlDocument to get IDispatch*,
IDispatchPtr spDisp=GetHtmlDocument();
Top
3 楼masterz(www.fruitfruit.com)回复于 2002-06-12 10:39:04 得分 0
you can use CHtmlView::GetHtmlDocument to get IDispatch*,
IDispatchPtr spDisp=GetHtmlDocument();
Top
4 楼guzh()回复于 2002-06-12 10:48:26 得分 0
你能给我一个源码吗?我用你的代码编译了一下,有很多错啊.
我的E-Mail是guzh@21cn.com 谢谢了.
f:/VC/ChangeHref/ChangeHref/MainFrm.cpp(109): error C2065: “IDispatchPtr” : 未声明的标识符
f:/VC/ChangeHref/ChangeHref/MainFrm.cpp(109): error C2065: “GetHtmlDocument” : 未声明的标识符
.....
等等.
Top
5 楼masterz(www.fruitfruit.com)回复于 2002-06-12 21:55:35 得分 100
void CChangeurlView::OnEditChangeurl()
{
// please note that you have to release those interface pointer
IDispatch* spDisp;
spDisp=GetHtmlDocument();
IHTMLDocument2* spDoc;
spDisp->QueryInterface(IID_IHTMLDocument2,(void**)&spDoc);
IHTMLElementCollection* pColl;
spDoc->get_anchors(&pColl);
long num;
HRESULT hr=pColl->get_length(&num);
for(long i=0;i<num;i++)
{
IDispatch* spDisp2;
pColl->item(_variant_t(i),_variant_t(i),&spDisp2);
IHTMLAnchorElement* spAnchor;
spDisp2->QueryInterface(IID_IHTMLAnchorElement,(void**)&spAnchor);
if(spAnchor)
spAnchor->put_href(_bstr_t("http://www.csdn.net"));
}
}
Top
6 楼masterz(www.fruitfruit.com)回复于 2002-06-13 09:10:47 得分 0
#include <mshtml.h> (download and install IE5.5 headers and libraries)
这篇关于在HtmlView中如何改变链接.(在线守候)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!