SlunkCrypt/gui/Utilities/WindowHelper.cs

68 lines
2.3 KiB
C#
Raw Normal View History

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<bool> 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 DisableMaximizeButton(this Window window)
{
if (!ReferenceEquals(window, null))
{
const int GWL_STYLE = -16;
const uint WS_MAXIMIZEBOX = 0x10000;
try
{
WindowInteropHelper interopHelper = new WindowInteropHelper(window);
uint value = NativeMethods.GetWindowLong(interopHelper.Handle, GWL_STYLE);
if ((value & WS_MAXIMIZEBOX) != 0U)
{
NativeMethods.SetWindowLong(interopHelper.Handle, GWL_STYLE, value & (~WS_MAXIMIZEBOX));
}
}
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);
}
}
}