WebBrowser控件的问题

来源:互联网 发布:iphone手机铃声mac 编辑:程序博客网 时间:2024/05/04 07:52

1。获得webbrowser控件中的html源代码

这里介绍如何用程序的方法获得WebBrowser控件中的HTML的源代码,并可以通过修改源代码内容来修改页面内容(注意:不是显示一个新的页面)。 

    首先要加入WebBrowser控件,加入控件的方面我就不说了。获得源代码方法有两种: 

一、方法1(严格说,这个方法只不过是调用WebBrowser自己的菜单命令"查看源文件而已",并非我们所希望的)
关键代码: 

#include "mshtmcid.h"
void CHtmlView::OnMethod1() 
{
    CWnd* pWnd = NULL; 

    CWnd* pWndShell = m_browser.GetWindow(GW_CHILD); // get the webbrowser window pointer

    if (pWndShell) 
    { 
       pWnd = pWndShell->GetWindow(GW_CHILD);  //get the child window pointer
    } 
    
    if (pWnd != NULL) 
    { 
       WPARAM wParam = MAKEWPARAM(IDM_VIEWSOURCE, 1);  //convert to unsigned 32 bit value and pass it to wparam
       pWnd->SendMessage(WM_COMMAND, wParam, (LPARAM)this->m_hWnd); //cool send a message to retreive the source.
    }         
}

二、方法2 

   原理在于取得IPersistStreamInit接口指针,然后把网页写到IStream流中去。
关键代码: 

#include "mshtml.h"
//在SourceView中填写HtmlView中网页的源程序
void CMainFrame::OnMethod2() 
{
    IHTMLDocument2 *pHTMLDocument=NULL;
    IPersistStreamInit *pPSI=NULL;

    IStream *pStream=NULL; 
    HGLOBAL  hHTMLText; 

    if  (!(pHTMLDocument = (IHTMLDocument2*)m_pHtmlView->m_browser.GetDocument()))
    return;
    
    if  (FAILED(pHTMLDocument->QueryInterface(&pPSI)))
    {
//   pHTMLDocument->Release();
    return;
    }    

    hHTMLText = GlobalAlloc(GMEM_FIXED, MAX_SIZE);
    CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream); 
    pPSI->Save(pStream, FALSE); 

//   m_pSourceView->SetWindowText((char*)hHTMLText);

    long nEditLength = m_pSourceView->GetEditCtrl().GetWindowTextLength();
    m_pSourceView->GetEditCtrl().SetSel(0, nEditLength);
    m_pSourceView->GetEditCtrl().ReplaceSel("");
    char *pText = (char*)hHTMLText;
    long lHtmlLength = strlen(pText);
    CString str("");
    long n = 0;
    for    (long i=0; i < lHtmlLength; i++)
    {
        if  (*pText != 0x0d && *pText != 0x0a)
        {
            str += *pText;
            pText++;
        }
        else
        {
            pText++;
            if  (*pText == 0x0a)
            pText++;
            str += "/r/n";
            nEditLength = m_pSourceView->GetEditCtrl().GetWindowTextLength();
            m_pSourceView->GetEditCtrl().SetSel(nEditLength, nEditLength);
            m_pSourceView->GetEditCtrl().ReplaceSel(str);
            str.Empty();
        }
    }

    pStream->Release();
    pPSI->Release();
//   pHTMLDocument->Release();
}

三、修改HTML源代码以改变网页的显示 

    这部分比较有意思,可以当作是一个小的HTML编辑器,看看预演效果。特别的不是显示一个新文件,而是修改原来的HTML文件。
关键代码: 

//根据SourceView里的HTML文本改变HtmlView里的显示
void CMainFrame::OnChangehtml() 
{
    IHTMLDocument2 *pHTMLDocument=NULL;
    IPersistStreamInit *pPSI=NULL;

    IStream *pStream=NULL; 
    HGLOBAL  hHTMLText; 

    if    (!(pHTMLDocument = (IHTMLDocument2*)m_pHtmlView->m_browser.GetDocument()))
        return;
    
    if    (FAILED(pHTMLDocument->QueryInterface(&pPSI)))
    {
//        pHTMLDocument->Release();
        return;
    }    

    pHTMLDocument->clear();
    pPSI->InitNew();

    LPCTSTR strText = m_pSourceView->LockBuffer();
    DWORD dwLength = strlen(strText);
    hHTMLText = GlobalAlloc(GMEM_FIXED, dwLength);
    memset(hHTMLText, 0, dwLength);
    memcpy(hHTMLText, strText, dwLength);
    m_pSourceView->UnlockBuffer();
    CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream); 
    ULARGE_INTEGER libNewSize;
    libNewSize.QuadPart = dwLength;
    pStream->SetSize(libNewSize);这一步必须要,否则显示时会有多余字符出现
    pPSI->Load(pStream); 

    pStream->Release();
    pPSI->Release();    
//    pHTMLDocument->Release();
}

    有时侯不能显示出网页而显示的是源码文本,比如微软网站的首页就是这种情况。把源码中的这句话 < META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1" />去掉就可以了。原因不明

2. WebBrowser隐藏后自动销毁的BUG以及解决办法

代码如下:
m_pWB=new CWebBrowser2;
m_nWBID = 7000;        
m_pWB[i]->Create(NULL,WS_CHILD | WS_VISIBLE,CRect (0,0,500,500),(CWnd *)(&m_tab1),m_nWBID,FALSE);    
m_pWB->Navigate("http://www.sina.com.cn/",NULL,NULL,NULL,NULL);

要显示和隐藏m_pWB,采用
m_pWB->SetVisible(false);
m_pWB->SetVisible(true);

m_pWB->ShowWindow(WS_HIDE)
m_pWB->ShowWindow(WS_SHOW)

都不能实现切换

WebBrowser控件有BUG
ShowWindow(WS_HIDE);之后控件就被销毁了。
再也显示不出来了:(

原因:

在MFC中,WebBrowser控件动态创建,没有设置WS_VISIBLE 属性 或者 调用了ShowWindow(SW_HIDE);
就会调用IOleControl::DoVerb() ,使WebBrowser控件的实例被销毁。


解决办法:


用SDK的ShowWindow函数。
//隐藏IE控件
::ShowWindow( m_pWB.GetSafeHwnd(), SW_HIDE );

//显示IE控件
::ShowWindow( m_pWB.GetSafeHwnd(), SW_SHOW );

原创粉丝点击