现在的位置: 首页 > 综合 > 正文

分析MFC中CDialog的子类对象如何知道自己是model还是modeless的

2012年12月28日 ⁄ 综合 ⁄ 共 1409字 ⁄ 字号 评论关闭
DoModal源代码关键部分如下
 1if (CreateDlgIndirect(lpDialogTemplate,
 2                        CWnd::FromHandle(hWndParent), hInst))
 3        {
 4            if (m_nFlags & WF_CONTINUEMODAL)
 5            {
 6                // enter modal loop
 7                DWORD dwFlags = MLF_SHOWONIDLE;
 8                if (GetStyle() & DS_NOIDLEMSG)
 9                    dwFlags |= MLF_NOIDLEMSG;
10                VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
11            }

相对于CDialog的Create,DoModal多了一个RunModalLoop()这么一个消息循环(当然也有一些别的,比如在创建窗口之前disable父窗口)。因此关键在这个函数里,看进去,在文件VC\atlmfc\src\mfc\wincore.cpp里

 1int CWnd::RunModalLoop(DWORD dwFlags)
 2{
 3    ASSERT(::IsWindow(m_hWnd)); // window must be created
 4    ASSERT(!(m_nFlags & WF_MODALLOOP)); // window must not already be in modal state
 5
 6    // for tracking the idle time state
 7    BOOL bIdle = TRUE;
 8    LONG lIdleCount = 0;
 9    BOOL bShowIdle = (dwFlags & MLF_SHOWONIDLE) && !(GetStyle() & WS_VISIBLE);
10    HWND hWndParent = ::GetParent(m_hWnd);
11    m_nFlags |= (WF_MODALLOOP|WF_CONTINUEMODAL);
12    MSG *pMsg = AfxGetCurrentMessage();
13
14    // acquire and dispatch messages until the modal state is done
15    for (;;)
16。。。。。。for内的消息之类的略掉。。。。
17ExitModal:
18    m_nFlags &= ~(WF_MODALLOOP|WF_CONTINUEMODAL);
19    return m_nModalResult;
20}

看到了吧,m_nFlags & WF_MODALLOOP就是用来判断本dialog是用DoModal还是直接Create创建的。
如果为true表示为model否则为modeless
注意:以上仅为现在MFC中CDialog中可用,其他lib,如WTL并不通用

抱歉!评论已关闭.