作者:朱金灿
来源:http://www.cnblogs.com/clever101
本文拙文《在对话框上创建视图的方法总结》,有网友朋友来邮件反映:在对话框上可以成功创建视图,但是用鼠标单击视图的时候,就会出现Assert错误,说ViewCore.cpp 252行中有错。具体是:CView类的onmouseActive函数中ASSERT(pParentFrame == pDesktopWnd || pDesktopWnd->IsChild(pParentFrame)),出错!
下班回家我决心亲自试验一番,我在我的对话框程序上添加了各种鼠标消息,包括WM_LBUTTONDOWN、WM_MOUSEMOVE和WM_LBUTTONUP,但是都没有出错。我一下抓瞎了,只好上论坛发贴求助,但是一时之间也没多少人回应。我只好直接在google上搜索ASSERT(pParentFrame == pDesktopWnd || pDesktopWnd->IsChild(pParentFrame))这个断言错误。终于搜到了一篇相关文章《如何在没有文档的情况下使用CView及其派生类》。阅读之下我算是明白了为什么会出错。首先我们明确断言错误是在CView::OnMouseActivate函数上发生的,那么让我们看看MSDN对OnMouseActivate函数的解释:
The framework calls this member function when the cursor is in an inactive window and the user presses a mouse button.
大意是当光标在一个非活动窗口和用户按下鼠标时框架就会调用这个消息。那我们再看看CView::OnMouseActivate函数的源码:
{
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
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;
}