You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB

using System.Runtime.InteropServices;
namespace EC.Util.Platform;
public class WinUtil
{
#region const
private const int GWL_STYLE = -16;
private const int WS_MAXIMIZEBOX = 0x10000;
private const int WS_MINIMIZEBOX = 0x20000;
private const int WS_SYSMENU = 0x80000;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_FRAMECHANGED = 0x0020;
#endregion const
#region lib
[DllImport("Kernel32")]
public static extern void AllocConsole();
[DllImport("Kernel32")]
public static extern void FreeConsole();
[DllImport("user32", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
private static extern int GetWindowLong32(IntPtr hWnd, int nIndex);
[DllImport("user32", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
private static extern int SetWindowLong32(IntPtr hwnd, int index, int value);
[DllImport("user32")]
private static extern int GetWindowLongPtr(IntPtr hwnd, int index);
[DllImport("user32")]
private static extern int SetWindowLongPtr(IntPtr hwnd, int index, int value);
[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
#endregion lib
#region cmd
public static void DisableMinimizeButton(IntPtr handle)
{
if (IntPtr.Size == 4)
{
int currentStyle = GetWindowLong32(handle, GWL_STYLE);
_ = SetWindowLong32(handle, GWL_STYLE, (currentStyle & ~WS_MINIMIZEBOX));
}
else
{
int currentStyle = GetWindowLongPtr(handle, GWL_STYLE);
_ = SetWindowLongPtr(handle, GWL_STYLE, (currentStyle & ~WS_MINIMIZEBOX));
}
//call SetWindowPos to make sure the SetWindowLongPtr take effect according to MSDN
SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
public static void DisableMaximizeButton(IntPtr handle)
{
if (IntPtr.Size == 4)
{
int currentStyle = GetWindowLong32(handle, GWL_STYLE);
_ = SetWindowLong32(handle, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX));
}
else
{
int currentStyle = GetWindowLongPtr(handle, GWL_STYLE);
_ = SetWindowLongPtr(handle, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX));
}
//call SetWindowPos to make sure the SetWindowLongPtr take effect according to MSDN
SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
public static void DisableMinMaximizeButton(IntPtr handle)
{
if (IntPtr.Size == 4)
{
int currentStyle = GetWindowLong32(handle, GWL_STYLE);
_ = SetWindowLong32(handle, GWL_STYLE, (currentStyle & ~WS_MINIMIZEBOX & ~WS_MAXIMIZEBOX));
}
else
{
int currentStyle = GetWindowLongPtr(handle, GWL_STYLE);
_ = SetWindowLongPtr(handle, GWL_STYLE, (currentStyle & ~WS_MINIMIZEBOX & ~WS_MAXIMIZEBOX));
}
//call SetWindowPos to make sure the SetWindowLongPtr take effect according to MSDN
SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
#endregion cmd
}