递归枚举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/archive/2011/08/01/2124313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值