在 MFC 中检测模态对话框

2022-01-12 00:00:00 modal-dialog mfc

如何以编程方式检测我的 MFC 应用程序当前是否正在显示模式对话框或属性表?目前我正在使用以下,但我觉得代码也会触发无模式对话框.

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.

bool HasModalDialog(const CWnd* pWnd)
{
   const CWnd* pChildWnd = pWnd ? pWnd->GetNextWindow(GW_HWNDPREV) : NULL;
   while (pChildWnd)
   {
      if (pWnd == pChildWnd->GetTopLevelParent() &&
         (pChildWnd->IsKindOf(RUNTIME_CLASS(CDialog)) ||
         pChildWnd->IsKindOf(RUNTIME_CLASS(CPropertySheet))))
      {
         return true;
      }

      pChildWnd = pChildWnd->GetNextWindow(GW_HWNDPREV);
   }

   return false;
}

用法:

HasModalDialog(AfxGetMainWnd())

谁有检测模态对话框的替代方法?

Anyone got a alternative way of detecting modal dialogs?

推荐答案

你试过了吗CWnd::GetLastActivePopup?

我还没有测试过它是否只适用于模式对话框.

I haven't tested this to see if it'll work for modal dialogs only.

编辑 1: 根据 Raymond Chen,GetLastActivePopup 应该返回当前活动的模态对话框.

Edit 1: According to Raymond Chen, GetLastActivePopup should return the current active modal dialog.

编辑 2: 检索当前模式窗口的另一种方法可能是修改代码以检查禁用的父/所有者 - 模式对话框应始终 在显示前禁用其所有者.

Edit 2: Perhaps another method to retrieve the current modal window would be to modify your code to check for a disabled parent/owner - modal dialogs should always disable their owner before displaying.

相关文章