怎样在程序中控制关闭DoModal产生的模态对话框

VincintCao 2002-12-02 01:05:16
1、在CMyBaseDialog::OnInitDialog()里面设置定时器
2、通过点击按钮以后产生模态对话框,
CMyBaseDialog::OnButton1()
{
CMyDlg dlg;
dlg.DoModal();
}
3、10秒到了,希望关闭本dialog(CMyBaseDialog),以及CMyDlg。
...全文
150 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
VincintCao 2002-12-02
  • 打赏
  • 举报
回复
to ysf1980()
的确,你的方法是正确的,而我在发这个问题之前,已经试过了该方法。但还是不行,模态对话框内的处理是这样的:
int CWnd::RunModalLoop(DWORD dwFlags)
{
ASSERT(::IsWindow(m_hWnd)); // window must be created
ASSERT(!(m_nFlags & WF_MODALLOOP)); // window must not already be in modal state

// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
BOOL bShowIdle = (dwFlags & MLF_SHOWONIDLE) && !(GetStyle() & WS_VISIBLE);
HWND hWndParent = ::GetParent(m_hWnd);
m_nFlags |= (WF_MODALLOOP|WF_CONTINUEMODAL);
MSG* pMsg = &AfxGetThread()->m_msgCur;

// acquire and dispatch messages until the modal state is done
for (;;)
{
ASSERT(ContinueModal());

// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE))
{
ASSERT(ContinueModal());

// show the dialog when the message queue goes idle
if (bShowIdle)
{
ShowWindow(SW_SHOWNORMAL);
UpdateWindow();
bShowIdle = FALSE;
}

// call OnIdle while in bIdle state
if (!(dwFlags & MLF_NOIDLEMSG) && hWndParent != NULL && lIdleCount == 0)
{
// send WM_ENTERIDLE to the parent
::SendMessage(hWndParent, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)m_hWnd);
}
if ((dwFlags & MLF_NOKICKIDLE) ||
!SendMessage(WM_KICKIDLE, MSGF_DIALOGBOX, lIdleCount++))
{
// stop idle processing next time
bIdle = FALSE;
}
}

// phase2: pump messages while available
do
{
ASSERT(ContinueModal());

// pump message, but quit on WM_QUIT
if (!AfxGetThread()->PumpMessage())
{
AfxPostQuitMessage(0);
return -1;
}

// show the window when certain special messages rec'd
if (bShowIdle &&
(pMsg->message == 0x118 || pMsg->message == WM_SYSKEYDOWN))
{
ShowWindow(SW_SHOWNORMAL);
UpdateWindow();
bShowIdle = FALSE;
}

if (!ContinueModal())
goto ExitModal;

// reset "no idle" state after pumping "normal" message
if (AfxGetThread()->IsIdleMessage(pMsg))
{
bIdle = TRUE;
lIdleCount = 0;
}

} while (::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE));
}

ExitModal:
m_nFlags &= ~(WF_MODALLOOP|WF_CONTINUEMODAL);
return m_nModalResult;
}
BOOL CWnd::ContinueModal()
{
return m_nFlags & WF_CONTINUEMODAL;
}

void CWnd::EndModalLoop(int nResult)
{
ASSERT(::IsWindow(m_hWnd));

// this result will be returned from CWnd::RunModalLoop
m_nModalResult = nResult;

// make sure a message goes through to exit the modal loop
if (m_nFlags & WF_CONTINUEMODAL)
{
m_nFlags &= ~WF_CONTINUEMODAL;
PostMessage(WM_NULL);
}
}
在EndModalLoop里面,我很怀疑是PostMessage所导致的问题。所以,我将以前的处理方法改了一下(原来的处理方法是在CMyBaseDialog::OnClose()里面去EndDialog的),改成先判断MyDlg窗口是否存在,存在就EndDialog,然后再关BaseDialog。结果就OK了。虽然我还是没有搞懂具体的错误所在。但是起码,没有问题了。
ysf1980 2002-12-02
  • 打赏
  • 举报
回复
你为何不在CMyBaseDialog中声明
类型为CMyDlg的成员变量
dlg.EndDialog()
VincintCao 2002-12-02
  • 打赏
  • 举报
回复
To hhuangchunlin(林之韵)
你可以试一下。结果不是你所想的。
hhuangchunlin 2002-12-02
  • 打赏
  • 举报
回复
你关掉CMyBaseDialog CMyDlg自动关掉了

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧