好的,所以我的previous question没有产生任何有用的答案,所以我会尝试从不同的方向来。
我的应用程序可能有几个窗口。给定屏幕坐标中的一个点,我需要找到它“落到”哪个窗口 - 即找到包含所述点的所有窗口中最重要的窗口。
如果他们在一个窗口中Visual
,我会使用VisualTreeHelper.HitTest
。但由于它们是不同的窗口,因此不清楚该方法的第一个参数是什么。
答案 0 :(得分:8)
使用纯WPF是不可能的,因为WPF不会公开其窗口的Z顺序。事实上,WPF努力维护窗户永远不会相互模糊的错觉。
如果您愿意进行Win32调用,解决方案很简单:
public Window FindWindowAt(Point screenPoint) // WPF units (96dpi), not device units
{
return (
from win in SortWindowsTopToBottom(Application.Current.Windows.OfType<Window>())
where new Rect(win.Left, win.Top, win.Width, win.Height).Contains(screenPoint)
select win
).FirstOrDefault();
}
public static IEnumerable<Window> SortWindowsTopToBottom(IEnumerable<Window> unsorted)
{
var byHandle = unsorted.ToDictionary(win =>
((HwndSource)PresentationSource.FromVisual(win)).Handle);
for(IntPtr hWnd = GetTopWindow(IntPtr.Zero); hWnd!=IntPtr.Zero; hWnd = GetWindow(hWnd, GW_HWNDNEXT))
if(byHandle.ContainsKey(hWnd))
yield return byHandle[hWnd];
}
const uint GW_HWNDNEXT = 2;
[DllImport("User32")] static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("User32")] static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
如果您的窗口可能是透明的,您还应该在FindWindowAt()的“where”子句中使用VisualTreeHelper.HitTest。