【发布时间】:2011-08-12 15:46:57
【问题描述】:
对于我处理的任何给定窗口,我需要一种方法来确定给定窗口是否为模态窗口。
据我所知,没有任何方法可以做到这一点,这就是为什么我需要一些聪明的解决方法来解决这个问题!
感谢您的帮助!
编辑:为什么我的 GetWindow(,GW_OWNER) 失败了? :(
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
[DllImport("user32.dll", ExactSpelling = true)]
internal static extern IntPtr GetAncestor(IntPtr hwnd, GetAncestor_Flags gaFlags);
[DllImport("user32.dll", SetLastError = false)]
internal static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
const UInt32 WS_DISABLED = 0x8000000;
internal enum GetAncestor_Flags
{
GetParent = 1,
GetRoot = 2,
GetRootOwner = 3
}
internal enum GetWindow_Cmd : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
IntPtr _inspHwnd = FindWindow("rctrl_renwnd32", inspector.Caption); // searching for a window with this name
if (_inspHwnd.ToInt32() != 0) // found window with this name
{
IntPtr _ownerHwnd = GetWindow(_inspHwnd, GetWindow_Cmd.GW_OWNER);
if (_ownerHwnd.ToInt32() != 0)
{
IntPtr _ancestorHwnd = GetAncestor(_ownerHwnd, GetAncestor_Flags.GetParent);
if (_ancestorHwnd == GetDesktopWindow())
{
if (GetWindowLong(_ancestorHwnd, -16) == WS_DISABLED)
{
// inspector is probably modal if you got all the way here
MessageBox.Show("modal flag tripped");
}
}
}
}
【问题讨论】:
-
只是在这里大声思考:1)寻找它的父窗口2)看看父窗口是否可以被激活
-
你好 boltclock,我对窗口属性的理解是有限的,所以我想问一下,在这种情况下“激活”是什么意思。是可见还是检查它是否存在?
-
这基本上意味着检查您是否可以使窗口聚焦(通常通过单击它或 Alt+Tabbing 到它)。
-
在非模态检查器上似乎'父'不等于outlook的主体,所以我不确定这种方法是否有效。
-
方法错误。如果您找到您正在寻找的 特定 窗口,那么它是否是模态的并不重要。
标签: c# winapi modal-dialog outlook-2007 inspector