using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace com.muldersoft.slunkcrypt.gui.utils { public static class WindowHelper { private static readonly IEnumerable enableTopmost = Array.AsReadOnly(new bool[] { true, false }); public static void BringWindowToTop(this Window window) { if (!ReferenceEquals(window, null)) { try { WindowInteropHelper interopHelper = new WindowInteropHelper(window); NativeMethods.BringWindowToTop(interopHelper.Handle); if (!NativeMethods.SetForegroundWindow(interopHelper.Handle)) { foreach (bool flag in enableTopmost) { window.Topmost = flag; } } } catch { } } } public static void EnableResize(this Window window, bool enable) { if (!ReferenceEquals(window, null)) { const int GWL_STYLE = -16; const uint WS_SIZEBOX = 0x40000; try { WindowInteropHelper interopHelper = new WindowInteropHelper(window); uint value = NativeMethods.GetWindowLong(interopHelper.Handle, GWL_STYLE); if (value != 0) { NativeMethods.SetWindowLong(interopHelper.Handle, GWL_STYLE, enable ? (value | WS_SIZEBOX) : (value & (~WS_SIZEBOX))); } } catch { } } } private static class NativeMethods { [DllImport("user32.dll", SetLastError = true)] public static extern bool BringWindowToTop(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern uint GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll", SetLastError = true)] public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); } } }