VC HTML IHtmlDocument相关代码片段

据说是根据InternetExplorer_Server窗口得到IHtmlDocument2接口

#include <mshtml.h>

#include <atlbase.h>

#include <oleacc.h>

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)

{

TCHAR buf[100];

::GetClassName( hwnd, (LPTSTR)&buf, 100 );

if ( _tcscmp( buf, _T("Internet EXPlorer_Server") ) == 0 ) /

{

*(HWND*)lParam = hwnd;

return FALSE;

}

else

return TRUE;

};

//You can store the interface pointer in a member variable

//for easier Access

void CDlg::OnGetDocInterface(HWND hWnd)

{

CoInitialize( NULL );

// EXPlicitly load MSAA so we know if it's installed

HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );

if ( hInst != NULL )

{

if ( hWnd != NULL )

{

HWND hWndChild=NULL;

// Get 1st document window

::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );

if ( hWndChild )

{

CComPtr<IHTMLDocument2> spDoc;

LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );

::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (Dword*)&lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );

if ( pfObjectFromLresult != NULL )

{

HRESULT hr;

hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );

if ( SUCCEEDED(hr) )

{

CComPtr<IDispatch> spDisp;

CComQIPtr<IHTMLWindow2> spWin;

spDoc->get_Script( &spDisp );

spWin = spDisp;

spWin->get_document( &spDoc.p );

// Change background color to red

spDoc->put_bgColor( CComVariant("red") );

}

}

} // else document not ready

} // else Internet EXPlorer is not running

::FreeLibrary( hInst );

} // else Active Accessibility is not installed

CoUninitialize();

}

 

想通过CHtmlView设定自己Cookie的代码,思路大致这样,但是下面代码还有些问题.

 CComBSTR bstr;
 // Cookie   
 // Read My Browser Cookie
    IDispatch *pDispCur = m_HtmlPage.GetHtmlDocument();
    CComPtr<IDispatch> pDispCurAuto(pDispCur);
    pDispCur->Release();
    CComPtr<IHTMLDocument2> pDoc2Cur;
    pDispCurAuto.QueryInterface(&pDoc2Cur);
 pDoc2Cur->get_cookie(&bstr);
    // Set Popup Browser Cookie
 CComPtr<IDispatch> pDispDoc;
 CComQIPtr<IHTMLWindow2> spWin;

 spWin->get_document(&pDispDoc.p);
  CComPtr<IHTMLDocument2> pDoc2; 
 pDispDoc.QueryInterface(&pDoc2);  
 pDoc2->put_cookie(bstr);

下面代码看起来像是实现遍历HTML 中的元素

BOOL         CParamDlg::LoadValues()  
  {  
          TRY{  
                  CHTMLElementCollection     ecAll=m_wndDhtmlEdit.GetDom().GetAll();  
                  long         lecLength=ecAll.GetLength();  
                  COleVariant   varIndex,varTemp;//var2   is   not   used   because   accessing   index,not   name  
                  varIndex.vt=VT_I4;  
                  CString   strElementID,strTemp;  
                  CHtmlparam*   pHtmlparam;  
                  CObject*         pOb;  
                  for(int   i=0;i<lecLength;i++){  
                          varIndex.lVal=i;  
                          CHTMLElement         el(ecAll.item(varIndex,varTemp));  
                          strTemp=HTMLElement_GetValueAttributeName(&el);  
                          if(!strTemp.IsEmpty()){//can   have   values  
                                  strElementID=HTMLElement_GetIDOrName(&el);  
                                  if(m_mapNameToParam.Lookup(strElementID,pOb)){//found  
                                          pHtmlparam=(CHtmlparam*)pOb;  
                                          el.setAttribute(strTemp,pHtmlparam->m_varVal,0);  
                                  }  
                          }  
                          strTemp=HTMLElement_GetToolTipAttributeName(&el);  
                          if(!strTemp.IsEmpty()){//can   have   ToolTip  
                                  varTemp=pHtmlparam->m_strAlt;  
                                  el.setAttribute(strTemp,varTemp,0);  
                          }  
   
                  }  
          }  
          CATCH(CException,pEx)  
          {  
                  CGlobal::ProcessException(pEx);  
                  return     FALSE;  
          }  
          END_CATCH  
          return     TRUE;  

}

 

下面代码是实现从 IHTMLDocument2 获得/写入 HTML 文本 (IPersistStreamInit)
// CHtmlView operations

BOOL CHtmlView::GetSource(CString& refString)
{
    BOOL bRetVal = FALSE;
    CComPtr<IDispatch> spDisp = GetHtmlDocument();

    if (spDisp != NULL)
    {
        HGLOBAL hMemory;
        hMemory = GlobalAlloc(GMEM_MOVEABLE, 0);
        if (hMemory != NULL)
        {
            CComQIPtr<IPersistStreamInit> spPersistStream = spDisp;
            if (spPersistStream != NULL)
            {
                CComPtr<IStream> spStream;
                if (SUCCEEDED(CreateStreamOnHGlobal(hMemory, TRUE, &spStream)))
                {
                    spPersistStream->Save(spStream, FALSE);

                    LPCTSTR pstr = (LPCTSTR) GlobalLock(hMemory);
                    if (pstr != NULL)
                    {
                        // Stream is always ANSI, but CString
                        // assignment operator will convert implicitly.

                        bRetVal = TRUE;
                        TRY
                        {                        
                            refString = pstr;
                        }
                        CATCH_ALL(e)
                        {
                            bRetVal = FALSE;
                            DELETE_EXCEPTION(e);
                        }
                        END_CATCH_ALL

                        if(bRetVal == FALSE)
                            GlobalFree(hMemory);
                        else
                            GlobalUnlock(hMemory);
                    }
                }
            }
        }
    }
    
    return bRetVal;
}


向 IHTMLDocument2  写入 HTML 文本


int CChildView::HqResize(void)
{
 USES_CONVERSION;
 CComPtr<IHTMLDocument2> pDoc;

 CComPtr<IHTMLElementCollection> sphtmlAll;
 CComPtr<IHTMLScriptElement> spObject;
 CComPtr<IDispatch> spDisp;
 CComVariant varName;
 CComVariant varIndex;

 if(FAILED(m_wndHq.GetDocument(&pDoc)) || pDoc==NULL)
  return 0;

 CString strHtml="<html><head><title>网页行情</title></head>"
  "<body leftmargin=0 topmargin=0>"
  "<OBJECT  ID=KYT CODEBASE='http://www.sostock.com.cn/hq/webhq/webhq.cab#version=1,0,0,5'"
  "CLASSID='clsid:C952403E-C18D-4332-9F3D-0E1D7C486145'"
  "ALIGN='CENTER'"
  "width='%d'"
  "height='%d'>"
  "</OBJECT>"
  "<script language=javascript id=KYT1>"
  "window.focus();"
  "</script>"
  "</body>"
  "</html>";


 CRect rc;
 GetClientRect(&rc);

 CString strIn;
 strIn.Format(strHtml,rc.Width()-20,rc.Height()-15);


 CComQIPtr<IPersistStreamInit> spPersistStream(pDoc);

 if(spPersistStream==NULL)
  return 0;


 LPTSTR lpMem = (LPTSTR)::GlobalAlloc( GPTR,strIn.GetLength()+1);
 lstrcpy(lpMem,strIn.GetBuffer());


 CComPtr<IStream>spStream;
 CreateStreamOnHGlobal( lpMem, TRUE, &spStream );
 // 初始化后,装载显示
 spPersistStream->InitNew();
 spPersistStream->Load(spStream );

 return 0;
}

 

你可能感兴趣的:(VC HTML IHtmlDocument相关代码片段)