private void UpdateZOrderOfThisAndOwner()
 {
     if (_updatingZOrder)
     {
         return;
     }
     try
     {
         _updatingZOrder = true;
         var windowInteropHelper = new WindowInteropHelper(this);
         var handle = windowInteropHelper.Handle;
         foreach (var shadowWindow in LoadedShadowWindows)
         {
             if (NativeMethods.GetWindow(new WindowInteropHelper(shadowWindow).Handle, 3) != handle)
             {
                 NativeMethods.SetWindowPos(new WindowInteropHelper(shadowWindow).Handle, handle, 0, 0, 0, 0, 19);
             }
             handle = new WindowInteropHelper(shadowWindow).Handle;
         }
         var owner = windowInteropHelper.Owner;
         if (!(owner != IntPtr.Zero))
         {
             return;
         }
         UpdateZOrderOfOwner(owner);
     }
     finally
     {
         _updatingZOrder = false;
     }
 }
        private void UpdateWindowPosCore()
        {
            var flags = 532;

            if (IsVisible)
            {
                flags |= 64;
            }
            else
            {
                flags |= 131;
            }
            NativeMethods.SetWindowPos(new WindowInteropHelper(this).Handle, IntPtr.Zero, (int)Left, (int)Top,
                                       (int)Width, (int)Height,
                                       flags);
        }
        private static void UpdateZOrderOfOwner(IntPtr hwndOwner)
        {
            var lastOwnedWindow = IntPtr.Zero;

            NativeMethods.EnumThreadWindows(NativeMethods.GetCurrentThreadId(), (hwnd, lParam) =>
            {
                if (NativeMethods.GetWindow(hwnd, 4) == hwndOwner)
                {
                    lastOwnedWindow = hwnd;
                }
                return(true);
            }, IntPtr.Zero);
            if (!(lastOwnedWindow != IntPtr.Zero) || !(NativeMethods.GetWindow(hwndOwner, 3) != lastOwnedWindow))
            {
                return;
            }
            NativeMethods.SetWindowPos(hwndOwner, lastOwnedWindow, 0, 0, 0, 0, 19);
        }