在MFC中检测模式对话框 [英] Detecting modal dialogs in 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?
推荐答案
Have you tried CWnd::GetLastActivePopup
?
我还没有测试它是否仅适用于模式对话框.
I haven't tested this to see if it'll work for modal dialogs only.
根据 Raymond Chen ,GetLastActivePopup
应该返回当前的活动模式对话框.
Edit 1: According to Raymond Chen, GetLastActivePopup
should return the current active modal dialog.
也许检索当前模式窗口的另一种方法是修改代码以检查禁用的父/所有者-模式对话框应始终
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.
这篇关于在MFC中检测模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!