再谈动态创建网页元素

 

关键字:IHTMLOptionElementFactory,IHTMLImageElementFactory,createElement

 

1、概述

在《FAQ:操纵下拉列表》中我曾写到如何调用IHTMLDocument2::createElementIHTMLSelectElement::add动态为IHTMLSelectElement添加表项:

 

  先调用IHTMLDocument2::createElement创建一个option对象,

  HRESULT createElement(          
      BSTR eTag,               
//标签名,可以是img, area, option(ie4), frame, iframe(ie5)

      IHTMLElement **newElem   //返回对象指针
  );

  再调用IHTMLSelectElement::add将创建的option对象添加到列表中

  HRESULT add(          
      IHTMLElement 
*
element,
      VARIANT before   
//添加到哪个位置,VT_I4类型的VARIANT

  );

 

方法来自

 

<SCRIPT LANGUAGE="JScript">
var oOption = document.createElement("OPTION");
oOption.text
="Ferrari"
;
oOption.value
="4"
;
oSelect.add(oOption);
</SCRIPT>

 

但事实上类似的代码在C++中却不能工作。

 

 

2、发现

写文章时测试过脚本调用之后就放在一边了,而真正写程序用到时才发现上面的问题。MFC中有个很好用的类CDHtmlDialog,提供了类似于CDialog的行为用以实现以HTML为表现形式的对话框,像Visual Studio 7/8的的Wizard,添加类等对话框就使用了类似的技术。CDHtmlDialog的核心在于MFC中被用得出神入化的宏,再加上一系列DDX函数来实现HTML content和应用程序的数据的数据交换。比如下面的函数可以实现读取某个能够匹配szIdIHTMLSelectElement中被选中的值,或选中value所指定的那个IHTMLOptionElement

 

void CDHtmlDialog::DDX_DHtml_SelectString(LPCTSTR szId, CString&  value, BOOL bSave)
{
    CComPtr
<IHTMLDocument2>
 sphtmlDoc;
    GetDHtmlDocument(
&
sphtmlDoc);
    
if (sphtmlDoc ==
 NULL)
        
return
;

    COleVariant varEmpty, varIndex;

    CComPtr
<IHTMLSelectElement>
 spSelect;
    CComPtr
<IDispatch>
 spdispOption;
    CComPtr
<IHTMLOptionElement>
 spOption;
    CComBSTR bstrText;
    HRESULT hr 
=
 S_OK;
    
long lIndex=-1
;

    hr 
= GetElementInterface(szId, __uuidof(IHTMLSelectElement), (void **&
spSelect);
    
if (spSelect ==
 NULL)
        
return
;

    
if
 (bSave)
    
{
        
// get the selected item

        value.Empty();
        spSelect
->get_selectedIndex(&
lIndex);
        
if (lIndex >= 0
)
        
{
            varIndex 
=
 lIndex;

            spSelect
->item(varIndex, varEmpty, &
spdispOption);
            
if
 (spdispOption)
            
{
                spdispOption
->QueryInterface(__uuidof(IHTMLOptionElement), (void **&
spOption);
                
if
 (spOption)
                
{
                    spOption
->get_text(&
bstrText);
                    
if
 (bstrText)
                        value 
=
 bstrText;
                }

            }

        }

    }

    
else
    
{
        bstrText.Attach(value.AllocSysString());
        lIndex 
=
 Select_FindString(spSelect, bstrText, FALSE);
        spSelect
->
put_selectedIndex(lIndex);
    }

}


 

MFC只提供了比较基本的交换函数,更复杂的的数据交换则需要自己实现了。比如我有一个CStringArray,希望能以CStringArray中的每个字符串为Value动态创建IHTMLOptionElement并添加到IHTMLSelectElement,或者将所有IHTMLSelectElementIHTMLOptionElementValue作为字符串保存到CStringArray中,该怎么办呢?

 

很简单啊,用文章开头说到的方法动态创建IHTMLOptionElement不就行了。但实际上虽然程序运行不保错,但MSHTML似乎没有任何反应。看来createElement只能用脚本调用了,《FAQ: 如何动态创建并访问网页元素》一文中讨论的问题也证明了这一点。同时,在那篇文章中能解决问题的insertAdjacentHTML对于IHTMLSelectElementIHTMLOptionElement也没有用。

 

 

3、方案

第一反应当然是上网搜了。我于是发现有个朋友在FAQ:操纵下拉列表》的CSDN文档中心版本作了如下的回复:

 

According to MSDN, “Before you can add an element to a collection, you must create it first by using  the IHTMLDocument2::createElement method”. Actually, it does not work.

See the detail 
in
 codeproject  

 

codeproject 的这篇文章,指的是《How to operate controls in an HTML file, using C++》,其中说到应该使用IHTMLOptionElementFactory来创建IHTMLOptionElement。而IHTMLOptionElementFactory需要从IHTMLWindow2(该文说应该从DocumentScript来获得IHTMLWindow2,其实从DocumentparentWindow更为直接)的Option来得到,再看MSDN,我们发现同样还有一个IHTMLImageElementFactory接口(从IHTMLWindow2Image得到)用于创建IHTMLImgElement。看来要么这两个接口有特殊的地方,要么就是历史遗留问题了。因为令人感到奇怪的是MSDN中关于IHTMLDocument2::createElement方法的说明对这两个接口只字不提:

 

In Microsoft Internet Explorer 4.0, the only new elements you can create are img, area, and option. As of Internet Explorer 5, you can create all elements programmatically, except for frame and iframe. In addition, the properties of these created elements are read/write and can be accessed programmatically. Before you use new objects, you must explicitly add them to their respective collections or to the document. To insert new elements into the current document, use the IHTMLDOMNode::insertBefore or IHTMLDOMNode::appendChild methods. 

Attributes can be included with the eTag as long as the entire string is valid HTML. 

 

 

4、示例

下面的函数演示了如何使用IHTMLOptionElementFactory来实现前面我们希望完成的功能,

 

void CMyDHtmlDialog::DDX_DHtml_SelectOptions(CDataExchange* pDX, LPCTSTR szId, CMIEStringArray&  value)
{
    CComPtr
<IHTMLDocument2>
 sphtmlDoc;
    GetDHtmlDocument(
&
sphtmlDoc);
    
if (sphtmlDoc ==
 NULL)
        
return
;

    COleVariant varEmpty, varIndex;

    HRESULT hr 
=
 S_OK;
    CComPtr
<IHTMLSelectElement>
 spSelect;
    hr 
= GetElementInterface(szId, __uuidof(IHTMLSelectElement), (void **&
spSelect);
    
if (spSelect ==
 NULL)
        
return
;

    
if (pDX->
m_bSaveAndValidate)
    
{
        
// get the selected item

        value.RemoveAll();
        
long length = 0
;
        spSelect
->get_length(&
length);

        
for (long lIndex = 0; lIndex < length; lIndex++
)
        
{
            varIndex 
=
 lIndex;
            CComPtr
<IDispatch>
 spdispOption;
            spSelect
->item(varIndex, varEmpty, &
spdispOption);
            
if
 (spdispOption)
            
{
                CComPtr
<IHTMLOptionElement>
 spOption;
                spdispOption
->QueryInterface(__uuidof(IHTMLOptionElement), (void **&
spOption);
                
if
 (spOption)
                
{
                    CComBSTR bstrText;
                    spOption
->get_text(&
bstrText);
                    
if
 (bstrText)
                    
{
                        value.Add(bstrText);
                    }

                }

            }

        }

    }

    
else
    
{
        
long length = 0
;
        spSelect
->get_length(&
length);
        
for (long lIndex = 0; lIndex < length; lIndex++
)
        
{
            spSelect
->remove(0
);
        }


        CComQIPtr
<IHTMLWindow2> spWindow;
        
if ( FAILED(sphtmlDoc->get_parentWindow(&spWindow)) || !
spWindow )
            
return
;

        CComQIPtr
<IHTMLOptionElementFactory>
 spOptionFactory;
        
if ( FAILED(spWindow->get_Option(&spOptionFactory)) || !
spOptionFactory )
            
return
;

        
// Add each items to selection

        for (long lIndex = 0; lIndex < value.GetCount(); lIndex++)
        
{
            CString strOption 
=
 value[lIndex];

            
// Get current item in the dictionary

            IHTMLOptionElement * pOption;
            VARIANT_BOOL vt_b 
= lIndex == 0 ?
 VARIANT_TRUE : VARIANT_FALSE;
            
if ( FAILED(spOptionFactory->
create(CComVariant(strOption), CComVariant(strOption), 
                CComVariant(vt_b), CComVariant(vt_b), 
&pOption)) || !
pOption )
                
continue
;

            
// Add to selection tag

            if ( FAILED(spSelect->add((IHTMLElement*)pOption, CComVariant(lIndex))) )
                
continue
;
        }

    }

}

 

 

参考文献:

FAQ:操纵下拉列表

FAQ: 如何动态创建并访问网页元素

CSDN技术中心 FAQ:操纵下拉列表

How to operate controls in an HTML file using C++
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值