BHO 动态添加元素
来源:互联网 发布:免费下载会声会影软件 编辑:程序博客网 时间:2024/06/11 22:11
[动态添加元素]
insertAdjacentHTML 方法:
(1) 得到 document 对象
// 创建 CComPtr类型对象 spDisp , spDisp 包含 IDISPATCH 指针
CComPtr<IDispatch> spDisp ;
HRESULT hr = this->mWebBrowser2->get_Document( &spDisp );
if ( SUCCEEDED(hr) && spDisp )
{
// 调用 spDisp 的 QueryInterface 方法, 获得 IHTMLDOCUMENT2 指针, 存储在 spDoc .
CComQIPtr< IHTMLDocument2 , &IID_IHTMLDocument2> spDoc( spDisp );
if ( spDoc )
{
CComPtr < IHTMLElement> pBodyElement ;
if ( SUCCEEDED( hr ) )
{
_bstr_t bstrState;
spDoc->get_readyState( &bstrState.GetBSTR());
if ( SUCCEEDED( hr ))
{
/*
* 检查文档是否加载完毕
*/
if ( 0 == strcmp ( (char *)bstrState, "complete") )
{
/*
* 此处添加元素
*/
}
}
}
}
(2) 动态添加元素((1)中已经得到 Document 对象 )
CComPtr<IHTMLElement> spBodyElem;
HRESULT hr = spDoc->get_body(&spBodyElem);
if ( FAILED(hr) || !spBodyElem )
return hr;
HRESULT hr ;
hr = spBodyElem->insertAdjacentHTML(L"afterBegin", _bstr_t("<img src="D://insert.bmp"></img>") );
(3) 验证是否添加成功:
CComPtr< IHTMLElement > pHtmlBody ;
_bstr_t strText;
spDoc->get_body( &pHtmlBody );
pHtmlBody->get_outerHTML( &strText.GetBSTR() ) ; //测试一下 strText 即可
createElement,appendChild 方法:
(1) 动态添加元素
HRESULT hr ;
CComPtr< IHTMLElement > pElement;
//
// 采用 createElment 添加图片成功
//
hr = pDoc->createElement( _bstr_t("<img src=/"D://new.bmp/"><img>"), &pElement );
if ( FAILED(hr) )
{
::AfxMessageBox("createElement Error") ;
return hr;
}
// 通过 Body 元素的结点
CComPtr<IHTMLElement> pBodyElem;
hr = pDoc->get_body(&pBodyElem);
if ( FAILED(hr) || !pBodyElem )
return hr ;
CComQIPtr< IHTMLDOMNode, &IID_IHTMLDOMNode > pBodyNode(pBodyElem);
if ( !pBodyNode )
return hr;
// 得到新建元素的结点
CComQIPtr< IHTMLDOMNode, &IID_IHTMLDOMNode > pNewChild(pElement);
CComPtr< IHTMLDOMNode > pRefChild;
hr = pBodyNode->appendChild( pNewChild, &pRefChild );
if ( FAILED(hr) || !pRefChild )
return hr;
(2)测试是否添加成功
CComPtr< IHTMLElementCollection > pElmentCollection;
long num;
pDoc->get_scripts( &pElmentCollection );
pElmentCollection ->get_length( &num );
_variant_t vName ;
_variant_t vIndex ;
vName.vt = VT_I4 ;
vIndex.vt = VT_I4 ;
long count, index ;
count = pElmentCollection->get_length( &count ) ;
for ( index = 0; index < count; index++ )
{
vIndex.lVal = index ;
vName.lVal = index ;
CComPtr<IDispatch > pDisp;
pElmentCollection->item( vName , vIndex , &pDisp);
CComQIPtr< IHTMLElement, &IID_IHTMLElement > pElement (pDisp);
_bstr_t strText;
pElement->get_tagName( &strText.GetBSTR() ); //检查 strText 值
pElement->get_outerHTML( &strText.GetBSTR() );
}
- BHO 动态添加元素
- BHO 动态添加元素
- JList动态添加元素
- 动态添加表单元素
- 动态添加DOM元素
- JList动态添加元素
- 动态添加元素例子
- JList动态添加元素
- 动态添加页面元素事件
- jQuery动态添加、删除元素
- 动态添加jquery Moblie元素
- javascript动态添加删除元素
- jquerymobile动态添加元素之后
- php jquery 动态添加元素
- Html元素动态添加方法
- jquery动态添加删除元素
- JS动态添加tr元素
- Dom动态添加删除元素
- Entity Framework 介绍
- 设计模式之—简单工厂方法(StaticFactory )-Java实现
- commons_dbutils的介绍与使用
- gridview
- 10位顶级PHP大师的开发原则
- BHO 动态添加元素
- HTTP状态码的含详细列表: 200:400:403:404:408:500
- java2d绘图教程
- Oracle数据库与MySQL数据库的几点区别
- c#数据库编程>>视频+教程
- 第四章
- 我思故我在系列—数据结构题NO.36(题目搜集整理者july,非常感谢!!)
- xend的安装路径
- Struts2中接收用户输入