cppwebbrower中获取frame中的源码(续)

先看代码,这部分主要是嵌套的frame源码获取

 
VARIANT                 id, index, id1, index1, index2;
 IDispatch               *spDispatch=0;
 IHTMLDocument2          *pDoc=NULL;
 IHTMLElement            *pElement=0;
 IHTMLElementCollection  *pElementCol=0;
 //  IHTMLDivElement        *pFormElement=0;
// IHTMLInputElement       *pInputElement=0;

 IHTMLWindow2 *pHTMLWnd = NULL; 
 IHTMLDocument2 *pFrameDoc=NULL; 
 IHTMLFramesCollection2 *pFramesCollection=NULL; 
 LPDISPATCH lpDispatch; 

 long p; 
 VARIANT varindex,varresult; 
 varresult.vt=VT_DISPATCH; 
 varindex.vt = VT_I4; 

    TStringList *Result = new  TStringList;

// if   ( !CppWebBrowser1->Busy   &&   CppWebBrowser1->Document   &&
//  AnsiString(CppWebBrowser1->LocationURL)   !=   "about:blank"  )
  //if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2,   (LPVOID*)&pDoc)) )
  if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2,   (LPVOID*)&pDoc)) )
   if(pDoc!=NULL) 
   { 
    HRESULT hr=pDoc->get_frames(&pFramesCollection); 
    if(SUCCEEDED(hr)&&pFramesCollection!=NULL) 
    { 
     hr=pFramesCollection->get_length(&p); 
     if(SUCCEEDED(hr)) 
      for(int i=0; i<p; i++) 
      { 
       varindex.lVal = i; 
       if(pFramesCollection->item(&varindex, &varresult) ==S_OK) 
       { 
        lpDispatch=(LPDISPATCH)varresult.ppdispVal; 
        if (SUCCEEDED(lpDispatch->QueryInterface(IID_IHTMLWindow2, (LPVOID *)&pHTMLWnd))) 
        {
         VARIANT framerequested;
         VARIANT frameout;
         IHTMLFramesCollection2* pframescol;
         IHTMLWindow2* prightframewindow;
         IHTMLDocument2* prightdoc;

         //framerequested.vt = VT_BSTR;//若为vt_i4则以索引号来访问
         //framerequested.bstrVal = L"printframe";//以名称来访问
         framerequested.vt = VT_I4;
         framerequested.bstrVal = (BSTR)i;

         hr = pHTMLWnd->get_frames(&pframescol);
         hr = pframescol->item(&framerequested, &frameout);

         //frameout.pdispVal
         hr = frameout.pdispVal->QueryInterface(IID_IHTMLWindow2, (void**)&prightframewindow);
         hr = prightframewindow->get_document(&prightdoc);
         
         //if(SUCCEEDED(pHTMLWnd->get_document( &pFrameDoc)))
         if (SUCCEEDED(hr))
         {
          //work with the pFrameDoc 
          if ( SUCCEEDED ( prightdoc->get_all ( &pElementCol ) ) )  // 得到所有元素
          {
           long  p = 0;
           if ( SUCCEEDED ( pElementCol->get_length ( &p ) ) )
           {
            // 哪一个Frame
            if ( p != 0 )
            {
             for ( long i = 0; i <= p-1; i++ )
             {
              V_VT ( &id ) = VT_I4;
              V_I4 ( &id ) = i;
              V_VT ( &index ) = VT_I4;
              V_I4 ( &index ) = 0;

              if ( SUCCEEDED ( pElementCol->item ( id, index, &spDispatch ) ) )
              {
               if
                (
                SUCCEEDED ( spDispatch->QueryInterface ( IID_IHTMLElement,
                ( void ** ) &pElement ) )
                )
               {

                 BSTR  Value;
                 BSTR  Type;
                 BSTR  Id;
                 BSTR  Name;

                 pElement->get_id ( &Id );             // 获取Id的方法不一样

                 AnsiString  tempID;
                 tempID = AnsiString ( Id ).UpperCase ();

                 CComBSTR bstrSrc;

                 pElement->get_outerHTML(&bstrSrc);

//                 LPTSTR   lpStr;
//                 _variant_t   varStr   =   "测试字符串 ";
//                 lpStr   =   _com_util::ConvertBSTRToString(   varStr.bstrVal   );

//                 Result->Add(tempID);
                 /*if (tempID.IsEmpty()) {
                                                                        
                 }
                 else   */
                 Result->Add(tempID + "------" + String(bstrSrc));
                 
//                 if ( tempID == "PRINTTABLE" )
//                 {
//
//                                                                            
//                  // WideString temp = SValue;
//                  //pInputElement->put_value ( (WideString) sUserName );
//                  return GetTablesRowsText2(pElement, strPhone1, strPhone2);
//                 }

 

               }
              }
             }
            }
           }
          }
         }
         pHTMLWnd->Release(); 
         pHTMLWnd=NULL; 
        } 
       } 
      } 
      pFramesCollection->Release(); 
    } 
    pDoc->Release(); 
   }


  return Result;


下面是一层frame源码的获取

VARIANT                 id, index, id1, index1;
	IDispatch               *spDispatch=0;
	IHTMLDocument2          *pDoc=NULL;
	IHTMLElement            *pElement=0;
	IHTMLElementCollection  *pElementCol=0;
	//		IHTMLDivElement        *pFormElement=0;
//	IHTMLInputElement       *pInputElement=0;

	IHTMLWindow2 *pHTMLWnd = NULL; 
	IHTMLDocument2 *pFrameDoc=NULL; 
	IHTMLFramesCollection2 *pFramesCollection=NULL; 
	LPDISPATCH lpDispatch; 

	long p; 
	VARIANT varindex,varresult; 
	varresult.vt=VT_DISPATCH; 
	varindex.vt = VT_I4; 

    TStringList *Result = new  TStringList;

//	if   ( !CppWebBrowser1->Busy   &&   CppWebBrowser1->Document   &&
//		AnsiString(CppWebBrowser1->LocationURL)   !=   "about:blank"  )
		//if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2,   (LPVOID*)&pDoc)) )
		if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2,   (LPVOID*)&pDoc)) )
			if(pDoc!=NULL) 
			{ 
				HRESULT hr=pDoc->get_frames(&pFramesCollection); 
				if(SUCCEEDED(hr)&&pFramesCollection!=NULL) 
				{ 
					hr=pFramesCollection->get_length(&p); 
					if(SUCCEEDED(hr)) 
						for(int i=0; i<p; i++) 
						{ 
							varindex.lVal = i; 
							if(pFramesCollection->item(&varindex, &varresult) ==S_OK) 
							{ 
								lpDispatch=(LPDISPATCH)varresult.ppdispVal; 
								if (SUCCEEDED(lpDispatch->QueryInterface(IID_IHTMLWindow2, (LPVOID *)&pHTMLWnd))) 
								{ 
									if(SUCCEEDED(pHTMLWnd->get_document( &pFrameDoc))) 
									{ 
										//work with the pFrameDoc 
										if ( SUCCEEDED ( pFrameDoc->get_all ( &pElementCol ) ) )  // 得到所有元素
										{
											long  p = 0;
											if ( SUCCEEDED ( pElementCol->get_length ( &p ) ) )
											{
												// 哪一个Frame
												if ( p != 0 )
												{
													for ( long i = 0; i <= p-1; i++ )
													{
														V_VT ( &id ) = VT_I4;
														V_I4 ( &id ) = i;
														V_VT ( &index ) = VT_I4;
														V_I4 ( &index ) = 0;

														if ( SUCCEEDED ( pElementCol->item ( id, index, &spDispatch ) ) )
														{
															if
																(
																SUCCEEDED ( spDispatch->QueryInterface ( IID_IHTMLElement,
																( void ** ) &pElement ) )
																)
															{

																	BSTR  Value;
																	BSTR  Type;
																	BSTR  Id;
																	BSTR  Name;

																	pElement->get_id ( &Id );             // 获取Id的方法不一样

																	AnsiString  tempID;
																	tempID = AnsiString ( Id ).UpperCase ();

																	CComBSTR bstrSrc;

																	pElement->get_outerHTML(&bstrSrc);

//																	LPTSTR   lpStr;
//																	_variant_t   varStr   =   "测试字符串 ";
//																	lpStr   =   _com_util::ConvertBSTRToString(   varStr.bstrVal   );

//																	Result->Add(tempID);
																	if (tempID.IsEmpty()) {
                                                                        
																	}
																	else
																	Result->Add(tempID + "------" + String(bstrSrc));
																	
//																	if ( tempID == "PRINTTABLE" )
//																	{
//
//                                                                            
//																		// WideString temp = SValue;
//																		//pInputElement->put_value ( (WideString) sUserName );
//																		return GetTablesRowsText2(pElement, strPhone1, strPhone2);
//																	}



															}
														}
													}
												}
											}
										}
									} 
									pHTMLWnd->Release(); 
									pHTMLWnd=NULL; 
								} 
							} 
						} 
						pFramesCollection->Release(); 
				} 
				pDoc->Release(); 
			}


	 return Result;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值