• 递归枚举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 );
                }
            }
        }

    }
  • 相关阅读:
    C#语言和SQL Server数据库技术_My Bank银行系统
    C#语言和SQL Server数据库技术_深入C#的String类
    C#语言和SQL Server数据库技术_C#语法快速热身
    HTML_利用CSS3制作网页动画
    HTML_定位网页元素
    HTML_浮动
    HTML_盒子模型
    HTML_css3美化网页元素
    iview中select搜索
    第六章、Vue项目预热
  • 原文地址:https://www.cnblogs.com/fangkm/p/1426530.html
Copyright © 2020-2023  润新知