Files
2026-02-21 16:45:37 +08:00

128 lines
4.0 KiB
C#

using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class WindowMod : MonoBehaviour
{
public Rect screenPosition;
public static DEVMODE startMode = default(DEVMODE);
public const int ENUM_CURRENT_SETTINGS = -1;
public const int CDS_UPDATEREGISTRY = 1;
public const int CDS_TEST = 2;
public const int DISP_CHANGE_SUCCESSFUL = 0;
public const int DISP_CHANGE_RESTART = 1;
public const int DISP_CHANGE_FAILED = -1;
public const int DISP_CHANGE_BADMODE = -2;
private const uint SWP_SHOWWINDOW = 64u;
private const int GWL_STYLE = -16;
private const int WS_BORDER = 1;
private const int WS_OVERLAPPED = 0;
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumDisplaySettings([MarshalAs(UnmanagedType.LPTStr)] string lpszDeviceName, [MarshalAs(UnmanagedType.U4)] int iModeNum, [In][Out] ref DEVMODE lpDevMode);
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int ChangeDisplaySettings([In][Out] ref DEVMODE lpDevMode, [MarshalAs(UnmanagedType.U4)] uint dwflags);
public void SetBorders()
{
SetWindowLong(GetForegroundWindow(), -16, 0);
bool flag = SetWindowPos(GetForegroundWindow(), 0, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, 64u);
}
public void SetBorderless()
{
SetWindowLong(GetForegroundWindow(), -16, 1);
bool flag = SetWindowPos(GetForegroundWindow(), 0, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, 64u);
}
public static void SaveStartMode()
{
Debug.Log("SaveStartMode");
EnumDisplaySettings(null, -1, ref startMode);
}
public static void LoadStartMode()
{
DEVMODE currentSettings = GetCurrentSettings();
Debug.Log("LoadStartMode: " + currentSettings.dmPelsWidth + " " + startMode.dmPelsWidth + " " + currentSettings.dmPelsHeight + " " + startMode.dmPelsHeight);
if (currentSettings.dmPelsWidth != startMode.dmPelsWidth || currentSettings.dmPelsHeight != startMode.dmPelsHeight)
{
ChangeDisplaySettings((int)startMode.dmPelsWidth, (int)startMode.dmPelsHeight);
}
}
public static void ChangeDisplaySettings(int width, int height)
{
DEVMODE lpDevMode = default(DEVMODE);
lpDevMode.dmSize = (ushort)Marshal.SizeOf(lpDevMode);
EnumDisplaySettings(null, -1, ref lpDevMode);
DEVMODE lpDevMode2 = lpDevMode;
lpDevMode2.dmPelsWidth = (uint)width;
lpDevMode2.dmPelsHeight = (uint)height;
int num = ChangeDisplaySettings(ref lpDevMode2, 0u);
switch (num)
{
case 0:
Debug.Log("Succeeded.\n");
GetCurrentSettings();
break;
case -2:
Debug.Log("Mode not supported.");
break;
case 1:
Debug.Log("Restart required.");
break;
default:
Debug.Log("Failed. Error code = " + num);
break;
}
}
public static void EnumerateSupportedModes()
{
DEVMODE lpDevMode = default(DEVMODE);
lpDevMode.dmSize = (ushort)Marshal.SizeOf(lpDevMode);
int i = 0;
Debug.Log("Supported Modes:");
for (; EnumDisplaySettings(null, i, ref lpDevMode); i++)
{
Debug.Log(string.Format("\t{0} by {1}, {2} bit, {3} degrees, {4} hertz", lpDevMode.dmPelsWidth, lpDevMode.dmPelsHeight, lpDevMode.dmBitsPerPel, lpDevMode.dmDisplayOrientation * 90, lpDevMode.dmDisplayFrequency));
}
}
public static DEVMODE GetCurrentSettings()
{
DEVMODE lpDevMode = default(DEVMODE);
lpDevMode.dmSize = (ushort)Marshal.SizeOf(lpDevMode);
if (EnumDisplaySettings(null, -1, ref lpDevMode))
{
Debug.Log(string.Format("Current Mode:\n\t{0} by {1}, {2} bit, {3} degrees, {4} hertz", lpDevMode.dmPelsWidth, lpDevMode.dmPelsHeight, lpDevMode.dmBitsPerPel, lpDevMode.dmDisplayOrientation * 90, lpDevMode.dmDisplayFrequency));
}
return lpDevMode;
}
}