使用CHtmlView时的问题
参考:https://blog.csdn.net/xiaolongwang2010/article/details/11100923
https://blog.csdn.net/xuepiaofei1/article/details/46930789
CRuntimeClass* prc = RUNTIME_CLASS(CHtmlView);
m_pHtmlView = DYNAMIC_DOWNCAST(CHtmlView, prc->CreateObject());
m_pHtmlView->Create(NULL, _T(""), WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), GetDlgItem(IDC_STATIC_HTML_VIEW), IDC_XXX);
m_pHtmlView->ShowWindow(SW_MAXIMIZE);
m_pHtmlView->Navigate("http://www.baidu.com", navNoHistory);
这段代码在OnInitDialog()里写的。
在基于Dialog的程序中,没有任何问题。
但是在基于单文档(SDI)的程序中,弹出一个dlg,里面嵌入一个HtmlView时,当点击该HtmlView时,会报一个断言错误(viewcore.cpp line270),然后再报一个断言错误(winfrm.cpp line1313),之后关闭该对话框,程序会崩溃。。。(因为SDI有CFrameWnd 窗口)
CScrollView的子类可能都有这个问题。
解决方案:
新建类 CMyHtmlView 继承 CHtmlView,使用类向导重新覆盖WM_MOUSEACTIVE消息响应函数
afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
int CMyHtmlView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
//return CHtmlView::OnMouseActivate(pDesktopWnd, nHitTest, message);
return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}
这样就跳过了CView::OnMouseActivate函数了,就不崩溃了
源码如下
int CView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
int nResult = CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
if (nResult == MA_NOACTIVATE || nResult == MA_NOACTIVATEANDEAT)
return nResult; // frame does not want to activate
CFrameWnd* pParentFrame = GetParentFrame();
if (pParentFrame != NULL)
{
// eat it if this will cause activation ******** Line 270 ******
ASSERT(pParentFrame == pDesktopWnd || pDesktopWnd->IsChild(pParentFrame));
// either re-activate the current view, or set this view to be active
CView* pView = pParentFrame->GetActiveView();
HWND hWndFocus = ::GetFocus();
if (pView == this &&
m_hWnd != hWndFocus && !::IsChild(m_hWnd, hWndFocus))
{
// re-activate this view
OnActivateView(TRUE, this, this);
}
else
{
// activate this view
pParentFrame->SetActiveView(this);
}
}
return nResult;
}
·