SlunkCrypt/gui/Utilities/WindowHelper.cs

44 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
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 { }
}
}
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);
}
}
}