Detecting modal dialogs in MFC

后端 未结 3 382
囚心锁ツ
囚心锁ツ 2021-01-13 20:53

How can I programmatically detect if my MFC application currently is displaying a modal dialog or property sheet? Currently I\'m using the following, but I feel that the cod

3条回答
  •  终归单人心
    2021-01-13 21:39

    If you are only detecting windows within your application then you could derive your own CDialog and CPropertySheet and put a simple bool in there that keeps track of whether it is modal or not.

    bool HasModalDialog(const CWnd* pWnd)
    {
       const CWnd* pChildWnd = pWnd ? pWnd->GetNextWindow(GW_HWNDPREV) : NULL;
       while (pChildWnd)
       {
          if (pWnd == pChildWnd->GetTopLevelParent() )
          {
             if ( pChildWnd->IsKindOf(RUNTIME_CLASS(CMyDialog) )
             {
                 return ((CMyDialog*)pChildWnd)->IsModal();
             }
    
             if ( pChildWnd->IsKindOf(RUNTIME_CLASS(CMyPropertySheet) )
             {
                 return ((CMyPropertySheet*)pChildWnd)->IsModal();
             }
          }
          pChildWnd = pChildWnd->GetNextWindow(GW_HWNDPREV);
       }
    
       return false;
    }
    

    There must be another way to do it but thats the only way I can think of off the top of my head.

提交回复
热议问题