MFC中的视图文档结构有CHtmlView,可以展示网页,其原理是IWebBrowser的封装,我们当然可以自己封装一个html控件,不多利用现有类来派生更加简单。
由于CHtmlView是视图文档结构中的一部分,需要CHtmlView视图从中剥离出来。只用重载下面三个函数。
1 BEGIN_MESSAGE_MAP(CHTMLCtrl, CHtmlView) 2 //{ {AFX_MSG_MAP(CHTMLCtrl) 3 ON_WM_DESTROY() 4 ON_WM_MOUSEACTIVATE() 5 //}}AFX_MSG_MAP 6 END_MESSAGE_MAP() 7 afx_msg void OnDestroy(); 8 afx_msg int OnMouseActivate(CWnd* pDesktopWnd,UINT nHitTest,UINT message); 9 10 virtual void PostNcDestroy() {};
1 //// 2 // Override to avoid CView stuff that assumes a frame. 3 // 4 void CHTMLCtrl::OnDestroy() 5 { 6 IHTMLDocument2Ptr pHTMLDoc2 = NULL; 7 8 pHTMLDoc2 = this->GetHtmlDocument(); 9 10 if (pHTMLDoc2) 11 { 12 pHTMLDoc2->Release(); 13 } 14 15 // This is probably unecessary since ~CHtmlView does it, but 16 // safer to mimic CHtmlView::OnDestroy. 17 if (m_pBrowserApp) 18 { 19 m_pBrowserApp->Release(); 20 m_pBrowserApp = NULL; 21 } 22 23 CWnd::OnDestroy(); // bypass CView doc/frame stuff 24 } 25 26 //// 27 // Override to avoid CView stuff that assumes a frame. 28 // 29 int CHTMLCtrl::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT msg) 30 { 31 // bypass CView doc/frame stuff 32 return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, msg); 33 }