现在的位置: 首页 > 综合 > 正文

如何利用CHtmlView类实现IE“查看->源文件”命令?

2013年10月11日 ⁄ 综合 ⁄ 共 1219字 ⁄ 字号 评论关闭

                                                            何志丹

一,新建一个单文档工程WatchSourceFile,视图类基于CHtmlView。
二,更改CWatchSourceFileView::OnInitialUpdate()函数,具体代码如下:
void CWatchSourceFileView::OnInitialUpdate()
{
 CHtmlView::OnInitialUpdate();
 Navigate2(_T("www.vcshare.net"),NULL,NULL);
}
三,在工具栏增加两个按钮ID_SEE1,IDD_SEE2,并在视力类为它们增加响应函数,具体代码如下:
#include   "mshtml.h"
#include   "atlbase.h"
void CWatchSourceFileView::OnSee1()
{
 CString    refString ;
    CComPtr<IDispatch>   spDisp   =   GetHtmlDocument();  
    if ( spDisp )
    {
        HGLOBAL   hMemory = GlobalAlloc(GMEM_MOVEABLE, 0);
        if( NULL != hMemory )
        {
            CComQIPtr<IPersistStreamInit>   spPersistStream   =   spDisp;
            if( spPersistStream )
            {
                CComPtr <IStream>   spStream;
                if(SUCCEEDED(CreateStreamOnHGlobal(hMemory,   TRUE,   &spStream)))
                {
                    spPersistStream-> Save(spStream,   FALSE);      
                    LPCTSTR   pstr   =   (LPCTSTR)   GlobalLock(hMemory);
                    if( pstr )
                    {                      
                        refString   =   pstr;
      AfxMessageBox(refString);
      GlobalUnlock(hMemory);
                    }
     ::GlobalFree(hMemory);
                }
            }
        }
    }  

抱歉!评论已关闭.