title: Detecting modal dialogs in MFC
body: <p>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 code also triggers for modeless dialogs.</p>
  
  <pre><code>bool HasModalDialog(const CWnd* pWnd)
  {
     const CWnd* pChildWnd = pWnd ? pWnd-&gt;GetNextWindow(GW_HWNDPREV) : NULL;
     while (pChildWnd)
     {
        if (pWnd == pChildWnd-&gt;GetTopLevelParent() &amp;&amp;
           (pChildWnd-&gt;IsKindOf(RUNTIME_CLASS(CDialog)) ||
           pChildWnd-&gt;IsKindOf(RUNTIME_CLASS(CPropertySheet))))
        {
           return true;
        }
  
        pChildWnd = pChildWnd-&gt;GetNextWindow(GW_HWNDPREV);
     }
  
     return false;
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>HasModalDialog(AfxGetMainWnd())
  </code></pre>
  
  <p>Anyone got a alternative way of detecting modal dialogs?</p>