递归枚举IHTMLDocument2的所有元素

  1. void  EnumHTMLDocument( MSHTML::IHTMLDocument2* pDoc )  
  2. {  
  3.     if( pDoc == NULL )return;  
  4.   
  5.     //遍历搜索子框架,递归处理子框架的文档  
  6.     CComPtr<MSHTML::IHTMLFramesCollection2>  spFramesCollection;  
  7.   
  8.     pDoc->get_frames( &spFramesCollection );  
  9.   
  10.     long lCount = 0;  
  11.     HRESULT hr = spFramesCollection->get_length( &lCount );  
  12.     if( FAILED( hr ) )return;  
  13.   
  14.     for ( long lIndex = 0; lIndex < lCount; lIndex++ )  
  15.     {  
  16.         CComVariant  vDispWin;  
  17.         vDispWin  = spFramesCollection->item( &CComVariant( lIndex ) );  
  18.   
  19.         CComQIPtr<MSHTML::IHTMLWindow2>  spWin = vDispWin.pdispVal;  
  20.         if( spWin == NULL )continue;  
  21.   
  22.         CComPtr<MSHTML::IHTMLDocument2> spSubDoc;  
  23.         spWin->get_document( &spSubDoc );  
  24.   
  25.         EnumHTMLDocument( spSubDoc );  
  26.     }  
  27.   
  28.     CComQIPtr<MSHTML::IHTMLElementCollection>  spElementCollection;  
  29.     hr  =  pDoc->get_forms( &spElementCollection );  
  30.     if( FAILED( hr ) )return;  
  31.   
  32.     long lFormCount = 0;  
  33.     hr  = spElementCollection->get_length( &lFormCount );  
  34.     if( FAILED( hr ) )return;  
  35.   
  36.     for ( long lIndex = 0; lIndex < lFormCount; lIndex++ )  
  37.     {  
  38.         CComQIPtr<MSHTML::IHTMLFormElement> spFormElement =   spElementCollection->item(  &CComVariant( lIndex ) );  
  39.         if( spFormElement == NULL )continue;  
  40.   
  41.         long lElemCount = 0;  
  42.         hr  = spFormElement->get_length( &lElemCount );  
  43.         if( FAILED( hr ) )continue;  
  44.   
  45.         for ( long lElemIndex = 0; lElemIndex < lElemCount; lElemIndex++ )  
  46.         {  
  47.             CComDispatchDriver  spInputElement;  
  48.             spInputElement  =  spFormElement->item( &CComVariant( lElemIndex ) );  
  49.             if( spInputElement == NULL )continue;  
  50.   
  51.             CComVariant varName, varValue, varType;  
  52.             hr = spInputElement.GetPropertyByName( L"name", &varName );  
  53.             if( SUCCEEDED( hr ) )  
  54.             {  
  55.                 LPCTSTR lpszName = varName.bstrVal ? COLE2CT( varName.bstrVal ) : _T("NULL");  
  56.                 AtlMessageBox( NULL, lpszName );  
  57.             }  
  58.   
  59.             hr = spInputElement.GetPropertyByName( L"value", &varValue );  
  60.             if( SUCCEEDED( hr ) )  
  61.             {  
  62.                 LPCTSTR lpszValue = varValue.bstrVal ? COLE2CT( varValue.bstrVal ) : _T("NULL");  
  63.                 AtlMessageBox( NULL, lpszValue );  
  64.             }  
  65.   
  66.             hr = spInputElement.GetPropertyByName( L"type", &varType );  
  67.             if( SUCCEEDED( hr ) )  
  68.             {  
  69.                 LPCTSTR lpszType = varType.bstrVal ? COLE2CT( varType.bstrVal ) : _T("NULL");  
  70.                 AtlMessageBox( NULL, lpszType );  
  71.             }  
  72.         }  
  73.     }  
  74.   

原文地址:https://www.cnblogs.com/maifengqiang/p/2124313.html