2022-10-29 00:23:06 +02:00
|
|
|
|
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 { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 22:29:11 +01:00
|
|
|
|
public static void DisableMaximizeButton(this Window window)
|
2022-10-30 15:18:51 +01:00
|
|
|
|
{
|
|
|
|
|
if (!ReferenceEquals(window, null))
|
|
|
|
|
{
|
|
|
|
|
const int GWL_STYLE = -16;
|
2022-11-10 00:09:23 +01:00
|
|
|
|
const ulong WS_MAXIMIZEBOX = 0x10000;
|
2022-10-30 15:18:51 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
WindowInteropHelper interopHelper = new WindowInteropHelper(window);
|
2022-11-10 00:09:23 +01:00
|
|
|
|
ulong value = NativeMethods.GetWindowLongPtr(interopHelper.Handle, GWL_STYLE);
|
|
|
|
|
if (value != 0UL)
|
2022-10-30 15:18:51 +01:00
|
|
|
|
{
|
2022-11-10 00:09:23 +01:00
|
|
|
|
NativeMethods.SetWindowLongPtr(interopHelper.Handle, GWL_STYLE, value & (~WS_MAXIMIZEBOX));
|
2022-10-30 15:18:51 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 00:23:06 +02:00
|
|
|
|
private static class NativeMethods
|
|
|
|
|
{
|
2022-11-10 00:09:23 +01:00
|
|
|
|
private const string DLL_USER32 = "user32.dll";
|
|
|
|
|
|
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "BringWindowToTop", ExactSpelling = true)]
|
2022-10-29 00:23:06 +02:00
|
|
|
|
public static extern bool BringWindowToTop(IntPtr hWnd);
|
|
|
|
|
|
2022-11-10 00:09:23 +01:00
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "SetForegroundWindow", ExactSpelling = true)]
|
2022-10-29 00:23:06 +02:00
|
|
|
|
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
2022-10-30 15:18:51 +01:00
|
|
|
|
|
2022-11-10 00:09:23 +01:00
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "GetWindowLongW", ExactSpelling = true)]
|
|
|
|
|
private static extern UIntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
|
|
|
|
|
|
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "GetWindowLongPtrW", ExactSpelling = true)]
|
|
|
|
|
private static extern UIntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
|
|
|
|
|
|
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "SetWindowLongW", ExactSpelling = true)]
|
|
|
|
|
private static extern UIntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, UIntPtr dwNewLong);
|
2022-10-30 15:18:51 +01:00
|
|
|
|
|
2022-11-10 00:09:23 +01:00
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)]
|
|
|
|
|
private static extern UIntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, UIntPtr dwNewLong);
|
|
|
|
|
|
|
|
|
|
public static ulong GetWindowLongPtr(IntPtr hWnd, int nIndex)
|
|
|
|
|
{
|
|
|
|
|
if (IntPtr.Size >= 8)
|
|
|
|
|
{
|
|
|
|
|
return GetWindowLongPtr64(hWnd, nIndex).ToUInt64();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return GetWindowLongPtr32(hWnd, nIndex).ToUInt64();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetWindowLongPtr(IntPtr hWnd, int nIndex, ulong dwNewLong)
|
|
|
|
|
{
|
|
|
|
|
if (IntPtr.Size >= 8)
|
|
|
|
|
{
|
|
|
|
|
SetWindowLongPtr64(hWnd, nIndex, new UIntPtr(dwNewLong));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetWindowLongPtr32(hWnd, nIndex, new UIntPtr(dwNewLong));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-29 00:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|