有些工程中需要在对话框中嵌套视图,这样的资料网络很多,下面一网友的代码
CRuntimeClass * pViewClass=RUNTIME_CLASS(CDemoView);
//生成一个CCreateContext对象备用
CCreateContext *pContext;
pContext=new CCreateContext;
pContext->m_pNewViewClass=pViewClass;
pContext->m_pCurrentDoc=NULL;
pContext->m_pNewDocTemplate=NULL;
pContext->m_pLastView=NULL;
pContext->m_pCurrentFrame=NULL;
//创建视图类对象,先从CObject*变换到CDemoView
m_pView = DYNAMIC_DOWNCAST(CDemoView, pViewClass->CreateObject());
m_pView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,CRect(0,0,0,0),this,0,pContext);
//CReateContext对象不再需要
delete pContext;
CRect rect;
GetDlgItem(IDC_RECT)->GetWindowRect(rect);
ScreenToClient(rect);
m_pView->MoveWindow(rect);
代码是没有问题的,在编译的时候鼠标点击会有异常,在RELEASE是没有问题,跟踪进去,发现是
ASSERT(pParentFrame == pDesktopWnd || pDesktopWnd-> IsChild(pParentFrame)),弹出的异常,进去该异常是OnMouseActivate,屏蔽就可以了,也可以用
return MA_ACTIVATE;
或者
return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);