zoukankan      html  css  js  c++  java
  • 递归枚举IHTMLDocument2的所有元素

    void  EnumHTMLDocument( MSHTML::IHTMLDocument2* pDoc )
    {
        
    if( pDoc == NULL )return;

        
    //遍历搜索子框架,递归处理子框架的文档
        CComPtr<MSHTML::IHTMLFramesCollection2>  spFramesCollection;

        pDoc
    ->get_frames( &spFramesCollection );

        
    long lCount = 0;
        HRESULT hr 
    = spFramesCollection->get_length( &lCount );
        
    if( FAILED( hr ) )return;

        
    for ( long lIndex = 0; lIndex < lCount; lIndex++ )
        {
            CComVariant  vDispWin;
            vDispWin  
    = spFramesCollection->item( &CComVariant( lIndex ) );

            CComQIPtr
    <MSHTML::IHTMLWindow2>  spWin = vDispWin.pdispVal;
            
    if( spWin == NULL )continue;

            CComPtr
    <MSHTML::IHTMLDocument2> spSubDoc;
            spWin
    ->get_document( &spSubDoc );

            EnumHTMLDocument( spSubDoc );
        }

        CComQIPtr
    <MSHTML::IHTMLElementCollection>  spElementCollection;
        hr  
    =  pDoc->get_forms( &spElementCollection );
        
    if( FAILED( hr ) )return;

        
    long lFormCount = 0;
        hr  
    = spElementCollection->get_length( &lFormCount );
        
    if( FAILED( hr ) )return;

        
    for ( long lIndex = 0; lIndex < lFormCount; lIndex++ )
        {
            CComQIPtr
    <MSHTML::IHTMLFormElement> spFormElement =    spElementCollection->item(  &CComVariant( lIndex ) );
            
    if( spFormElement == NULL )continue;

            
    long lElemCount = 0;
            hr  
    = spFormElement->get_length( &lElemCount );
            
    if( FAILED( hr ) )continue;

            
    for ( long lElemIndex = 0; lElemIndex < lElemCount; lElemIndex++ )
            {
                CComDispatchDriver  spInputElement;
                spInputElement  
    =  spFormElement->item( &CComVariant( lElemIndex ) );
                
    if( spInputElement == NULL )continue;

                CComVariant varName, varValue, varType;
                hr 
    = spInputElement.GetPropertyByName( L"name"&varName );
                
    if( SUCCEEDED( hr ) )
                {
                    LPCTSTR lpszName 
    = varName.bstrVal ? COLE2CT( varName.bstrVal ) : _T("NULL");
                    AtlMessageBox( NULL, lpszName );
                }

                hr 
    = spInputElement.GetPropertyByName( L"value"&varValue );
                
    if( SUCCEEDED( hr ) )
                {
                    LPCTSTR lpszValue 
    = varValue.bstrVal ? COLE2CT( varValue.bstrVal ) : _T("NULL");
                    AtlMessageBox( NULL, lpszValue );
                }

                hr 
    = spInputElement.GetPropertyByName( L"type"&varType );
                
    if( SUCCEEDED( hr ) )
                {
                    LPCTSTR lpszType 
    = varType.bstrVal ? COLE2CT( varType.bstrVal ) : _T("NULL");
                    AtlMessageBox( NULL, lpszType );
                }
            }
        }

    }
  • 相关阅读:
    jquery常用操作@测试分享
    selenium 上传文件
    python 安装mysql驱动
    创建react项目
    入栈操作的合法性 【重复元素】
    git笔记
    python GUI实战项目——tkinter库的简单实例
    Excel更改单元格格式后无效
    Find the Difference
    Two Sum IV
  • 原文地址:https://www.cnblogs.com/fangkm/p/1426530.html
Copyright © 2011-2022 走看看