如何按 z-index 对 Windows 进行排序? [英] How to sort Windows by z-index?
本文介绍了如何按 z-index 对 Windows 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在 Application.Current.Windows
中枚举窗口,我怎么知道对于任意两个窗口,哪个更接近"(即具有更大的 z-index)?>
或者,换句话说,我如何按 z-index 对这些窗口进行排序?
解决方案
您无法从 WPF 获取 Window 的 Z 顺序信息,因此您必须求助于 Win32.
这样的事情应该可以解决问题:
var topToBottom = SortWindowsTopToBottom(Application.Current.Windows.OfType());...公共 IEnumerableSortWindowsTopToBottom(IEnumerable未排序){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);
它的工作方式是:
- 它使用字典通过窗口句柄索引给定的窗口,使用的事实是在 WPF 的 Windows 实现中,Window 的 PresentationSource 始终是 HwndSource.
- 它使用 Win32 从上到下扫描所有无父窗口以找到正确的顺序.
If I'm enumerating windows in Application.Current.Windows
, how can I tell, for any two windows, which one is "closer" (i.e. has greater z-index)?
Or, to say the same in other words, how can I sort those windows by z-index?
解决方案
You cannot get a Window's Z Order information from WPF so you must resort to Win32.
Something like this ought to do the trick:
var topToBottom = SortWindowsTopToBottom(Application.Current.Windows.OfType<Window>());
...
public 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);
The way this works is:
- It uses a dictionary to index the given windows by window handle, using the fact that in the Windows implementation of WPF, Window's PresentationSource is always HwndSource.
- It uses Win32 to scan all unparented windows top to bottom to find the proper order.
这篇关于如何按 z-index 对 Windows 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文