Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort Windows by z-index?

Tags:

wpf

z-index

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?

like image 961
Fyodor Soikin Avatar asked Aug 13 '10 00:08

Fyodor Soikin


1 Answers

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:

  1. 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.
  2. It uses Win32 to scan all unparented windows top to bottom to find the proper order.
like image 66
Ray Burns Avatar answered Sep 30 '22 14:09

Ray Burns