如果我在枚举窗口Application.Current.Windows,我怎么能知道,对于任何两个窗口,哪一个"更接近"(即具有更大的z-index)?
或者,换句话说,我怎样才能通过z-index对这些窗口进行排序?
您无法从WPF获取Window的Z Order信息,因此您必须使用Win32.
像这样的东西应该做的伎俩:
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);
Run Code Online (Sandbox Code Playgroud)
这种方式的工作原理是:
| 归档时间: |
|
| 查看次数: |
6374 次 |
| 最近记录: |