(1)新建一工程文件:ModeFrame,选取MFC AppWizard(exe)。
(2)第二步选取Single document(单文档)。
(3)其余几步均为缺省值。
(4)用ClassWizard添加一新类CSubModeFrame,以CFrameWnd为基类。
//在CsubModeFrame中添加的实现函数DoModal()
int CSubModeFrame::DoModal()
{
HWND hWndParent = AfxGetApp()->m_pMainWnd->m_hWnd;
CRect rc(0,0,400,400);
CWnd *pParent = CWnd::FromHandle(hWndParent);
DWORD dwStyle = WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_POPUP|WS_THICKFRAME|WS_VISIBLE|WS_SYSMENU|WS_CAPTION;
if(!Create(NULL,"模态文档/试图框架 ",dwStyle,rc,pParent,NULL))
return FALSE;
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent,FALSE);
::EnableWindow(m_hWnd,TRUE);
bEnableParent = TRUE;
}
CenterWindow();
TRY
{
// enter modal loop
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}
CATCH_ALL(e)
{
//DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL
if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);
// destroy modal window
DestroyWindow();
return m_nModalResult;
}
//在CsubModeFrame中添加的实现函数EndModal()
void CSubModeFrame::EndModal()
{
ASSERT(::IsWindow(m_hWnd));
if(m_nFlags & (WF_MODALLOOP|WF_CONTINUEMODAL))
{
EndModalLoop(1);
}
}
//添加CModeFrameView的实现函数OnLButtonDblClk()
void CModeFrameView::OnView()
{
// TODO: Add your command handler code here
CSubModeFrame SubModeFrame;
if(SubModeFrame.DoModal())
{
MessageBox("Mode Ok");
}
//编译运行工程,双击视图,就会弹出模态的子文档/视图框架结构