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




从 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> 

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

多大


http://blog.vckbase.com/zaboli/archive/2005/06/23/7138.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取IWebBrowser2中的元素id,可以使用以下步骤: 1. 通过IWebBrowser2::get_Document方法获取IHTMLDocument2接口。 2. 使用IHTMLDocument2::get_all方法获取IHTMLElementCollection接口,并使用它来搜索指定的元素。 3. 在IHTMLElementCollection中使用IHTMLElementCollection::item方法获取指定项的IHTMLElement接口。 4. 在IHTMLElement中使用IHTMLElement::get_id方法获取元素的id属性。 下面是一个示例代码: ``` IWebBrowser2* pWebBrowser = // 获取WebBrowser控件指针 IHTMLDocument2* pHTMLDoc = NULL; pWebBrowser->get_Document((IDispatch**)&pHTMLDoc); // 获取所有元素 IHTMLElementCollection* pAllElements = NULL; pHTMLDoc->get_all(&pAllElements); // 搜索指定元素 VARIANT vIndex; vIndex.vt = VT_I4; vIndex.lVal = 0; BSTR bstrTagName = L"input"; BSTR bstrType = L"text"; BSTR bstrName = L"name"; IHTMLElement* pInputElement = NULL; while (pAllElements->item(bstrTagName, vIndex, (IDispatch**)&pInputElement) == S_OK) { // 检查元素的type和name属性 CComBSTR bstrTypeValue; CComBSTR bstrNameValue; pInputElement->get_type(&bstrTypeValue); pInputElement->get_name(&bstrNameValue); if (bstrTypeValue == bstrType && bstrNameValue == bstrName) { // 获取元素的id属性 CComBSTR bstrIdValue; pInputElement->get_id(&bstrIdValue); // 在这里使用元素的id属性 break; } // 下一个元素 vIndex.lVal++; pInputElement->Release(); } pAllElements->Release(); ``` 注意,在使用完IHTMLElement接口后必须调用Release方法来释放它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值