Window ZIndex

namespace Core.Utility
{
   using System;
   using System.Runtime.InteropServices;
   using System.Windows;
   using System.Windows.Interop;

   /// <summary>
   /// Class WindowZIndex.
   /// </summary>
   /// <remarks>
   /// Providers a way for window z-index to be set
   /// </remarks>
   public static class WindowZIndex
   {
      [DllImport("user32.dll")]
      private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

#pragma warning disable SA1201 // Elements must appear in the correct order
#pragma warning disable SA1310 // Field names must not contain underscore
      private const int SWP_NOSIZE = 0x0001;
      private const int SWP_NOMOVE = 0x0002;
      private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
      private static readonly IntPtr HWND_TOP = new IntPtr(0);
#pragma warning restore SA1310 // Field names must not contain underscore
#pragma warning restore SA1201 // Elements must appear in the correct order

      /// <summary>
      /// Sends to back the selected window back in the zindex
      /// </summary>
      /// <param name="window">The window.</param>
      public static void SendToBack(Window window)
      {
         if (window == null)
         {
            return;
         }

         var hWnd = new WindowInteropHelper(window).Handle;
         SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
      }

      /// <summary>
      /// Brings to front of the zindex
      /// </summary>
      /// <param name="window">The window.</param>
      public static void BringToFront(Window window)
      {
         if (window == null)
         {
            return;
         }

         var hWnd = new WindowInteropHelper(window).Handle;
         SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
      }
   }
}