从 IHTMLDocument2 获得/写入 HTML 文本 (IPersistStreamInit)

 
从 IHTMLDocument2 获得/写入 HTML 文本 (IPersistStreamInit)

void CUxHtmlViewerView::SetScript(LPCTSTR lpszTxt)
{
    Navigate(_T("about:blank"),NULL,NULL,NULL,NULL);
    HRESULT hr = E_NOINTERFACE;
    CComPtr<IHTMLDocument2> spHTMLDocument = static_cast<IHTMLDocument2*>(this->GetHtmlDocument());
    CStreamOnCString stream(lpszTxt);

    CComQIPtr<IPersistStreamInit> spPSI;

    if (spHTMLDocument)
    {
        spPSI = spHTMLDocument;
        if (spPSI)
            hr = spPSI->Load(static_cast<IStream*>(&stream));
    }
}

/////////////////////////////////////////////////////////////////////////////
// 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;
}

 

 

 先打开一个 about:blank 空白页面才能得到 HtmlDocument


int CHotDlgNews::NewMsg(CString strMsg)
{
m_ocxMsg.Navigate(("about:blank"),NULL,NULL,NULL,NULL);

CComPtr<IDispatch> spDisp = m_ocxMsg.get_Document();

  if(spDisp != NULL)
{
CComQIPtr<IPersistStreamInit> spPersistStream(spDisp);
if (spPersistStream != NULL)
{
LPTSTR lpMem = (LPTSTR)::GlobalAlloc( GPTR,strMsg.GetLength()+1);
lstrcpy(lpMem,strMsg.GetBuffer());


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


m_strSms=strMsg;
m_vMsgVector.push_back(strMsg);
UpdateData(FALSE);
ShowWindow(SW_SHOW);
this->SetFocus();

return 0;
}

 

今天用这个方法 向 IE 控件写入 HTML代码,可是在 win2000 pro ie 6 的机器上死活只显示 HTML 源代码,不出页面效果。。。。折腾了一天。。后来发现要把HTML代码写全他才认。。。
我只写了
<div>你好</div><br><div>你好</div> 他就不认识。。。
后来写成:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body><div>你好</div><br>< div>你好</div></body></html>

他就认识了。。。晕~~ 好弱智。。。

 

 

你可能感兴趣的:(JavaScript,html,exception,Stream,null,delete)