using System; using System.Collections; #if UNITY_STANDALONE_WIN using System.Runtime.InteropServices; #endif using UnityEngine; namespace GeneralTools { public class WindowMod : SingletonBaseAttribute { #if UNITY_STANDALONE_WIN [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags); [DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll", EntryPoint = "SetWindowLong")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("User32.dll", EntryPoint = "GetWindowLong")] private static extern int GetWindowLong(IntPtr hWnd, int dwNewLong); [DllImport("User32.dll", EntryPoint = "MoveWindow")] private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint); [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)] public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP); [DllImport("user32.dll", EntryPoint = "SetParent", CharSet = CharSet.Auto)] public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent); [DllImport("user32.dll", EntryPoint = "GetParent", CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hChild); [DllImport("User32.dll", EntryPoint = "GetSystemMetrics")] public static extern IntPtr GetSystemMetrics(int nIndex); [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] public static extern bool SetForegroundWindow(IntPtr hWnd); public enum appStyle { WindowedFullScreen = 0, Windowed = 1, WindowedWithoutBorder = 2, } protected appStyle AppWindowStyle = appStyle.WindowedFullScreen; protected int AppWindowStyleInt = 0; public enum zDepth { Bottom = -1, Normal = 0, Top = 1, TopMost = 2, } protected zDepth ScreenDepth = zDepth.Normal; protected int windowLeft = 0; protected int windowTop = 0; private int windowWidth = Screen.width; private int windowHeight = Screen.height; const uint SWP_SHOWWINDOW = 0x0040; const int GWL_STYLE = -16; const int WS_BORDER = 1; const int SW_SHOWRESTORE = 1;//还原 const int SW_SHOWMINIMIZED = 2;//(最小化窗口) const int SW_SHOWMAXIMIZED = 3;//最大化 private Rect screenPosition; private const int GWL_EXSTYLE = -20; private const int WS_CAPTION = 0xC00000; private const int WS_POPUP = 0x800000; IntPtr HWND_BOTTOM = new IntPtr(1); IntPtr HWND_NORMAL = new IntPtr(-2); IntPtr HWND_TOP = new IntPtr(0); IntPtr HWND_TOPMOST = new IntPtr(-1); private const int SM_CXSCREEN = 0x00000000; private const int SM_CYSCREEN = 0x00000001; int Xscreen; int Yscreen; IntPtr hWnd; public override void IAwake() { hWnd = FindWindow(null, Application.productName); windowWidth = ConfigHelper.screen_width; windowHeight = ConfigHelper.screen_height; windowLeft = ConfigHelper.window_Left; windowTop = ConfigHelper.window_Top; AppWindowStyle = (appStyle)ConfigHelper.fullscreen; ScreenDepth = (zDepth)ConfigHelper.screenDepth; InitScreen(); } public override void IStart() { base.IStart(); KeyDownManager.Instance.AddKeyDownAction(KeyCode.LeftArrow, () => { ChangeScreenPosition(new Vector2(-5, 0)); }, "窗口左移", true, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.RightArrow, () => { ChangeScreenPosition(new Vector2(5, 0)); }, "窗口右移", true, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.UpArrow, () => { ChangeScreenPosition(new Vector2(0, -5)); }, "窗口上移", true, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.DownArrow, () => { ChangeScreenPosition(new Vector2(0, 5)); }, "窗口下移", true, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.F9, () => { InitScreen(); }, "窗口初始化", false, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.F10, () => { SetMinWindow(); }, "最小化", false, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.F11, () => { SetMaxWindow(); }, "最大化", false, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.F12, () => { SetNormalWindow(); }, "还原", false, 0); KeyDownManager.Instance.AddKeyDownAction(KeyCode.LeftControl, () => { Cursor.visible = !Cursor.visible; }, "显示/隐藏鼠标", false, 0); } void InitScreen() { if (Application.isEditor) { return; } Xscreen = (int)GetSystemMetrics(SM_CXSCREEN); Yscreen = (int)GetSystemMetrics(SM_CYSCREEN); if (AppWindowStyle == 0) { Screen.SetResolution(Xscreen, Yscreen, true); } else if ((int)AppWindowStyle == 1) { Screen.SetResolution(windowWidth, windowHeight, false); } else if ((int)AppWindowStyle == 2) { Screen.SetResolution(windowWidth, windowHeight, false); screenPosition = new Rect(windowLeft, windowTop, windowWidth, windowHeight); } StartCoroutine(IE_SetWindows()); Cursor.visible = ConfigHelper.showcursor; Application.targetFrameRate = ConfigHelper.targetFramerate; } IEnumerator IE_SetWindows() { yield return new WaitForEndOfFrame(); yield return new WaitForFixedUpdate(); SetWindows(); } private void ChangeScreenPosition(Vector2 pos) { screenPosition.x += pos.x; screenPosition.y += pos.y; StartCoroutine(IE_SetWindows()); } private void SetWindows() { if (hWnd == null) { Debug.LogError("未获取到窗口句柄"); return; } if (AppWindowStyle == 0) { SetWindowLong(hWnd, -16, 369164288); if ((int)ScreenDepth == -1) SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, (int)GetSystemMetrics(SM_CXSCREEN), (int)GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW); if (ScreenDepth == 0) SetWindowPos(hWnd, HWND_NORMAL, 0, 0, (int)GetSystemMetrics(SM_CXSCREEN), (int)GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW); if ((int)ScreenDepth == 1) SetWindowPos(hWnd, HWND_TOP, 0, 0, (int)GetSystemMetrics(SM_CXSCREEN), (int)GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW); if ((int)ScreenDepth == 2) SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, (int)GetSystemMetrics(SM_CXSCREEN), (int)GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW); } if ((int)AppWindowStyle == 1) { if ((int)ScreenDepth == -1) { SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, 0x0001 | 0x0002); SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020); } if (ScreenDepth == 0) { SetWindowPos(hWnd, HWND_NORMAL, 0, 0, 0, 0, 0x0001 | 0x0002); SetWindowPos(hWnd, HWND_NORMAL, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020); } if ((int)ScreenDepth == 1) { SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, 0x0001 | 0x0002); SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020); } if ((int)ScreenDepth == 2) { SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002); SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020); } } if ((int)AppWindowStyle == 2) { SetWindowLong(hWnd, -16, 369164288); if ((int)ScreenDepth == -1) SetWindowPos(hWnd, HWND_BOTTOM, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); if (ScreenDepth == 0) SetWindowPos(hWnd, HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); if ((int)ScreenDepth == 1) SetWindowPos(hWnd, HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); if ((int)ScreenDepth == 2) SetWindowPos(hWnd, HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); } } //最小化窗口 public void SetMinWindow() { if (hWnd == null) { Debug.LogError("未获取到窗口句柄"); } if (!Application.isEditor) { ShowWindow(hWnd, SW_SHOWMINIMIZED); } } //恢复窗口 public void SetNormalWindow() { if (hWnd == null) { Debug.LogError("未获取到窗口句柄"); } if (!Application.isEditor) { ShowWindow(hWnd, SW_SHOWRESTORE); } } //最大化窗口 public void SetMaxWindow() { if (hWnd == null) { Debug.LogError("未获取到窗口句柄"); } if (!Application.isEditor) { ShowWindow(hWnd, SW_SHOWMAXIMIZED); } } //置底窗口 public void SetWindowBottom() { ScreenDepth = zDepth.Bottom; InitScreen(); } //置顶窗口 public void SetWindowTop() { ScreenDepth = zDepth.Top; InitScreen(); } //永久置顶窗口 public void SetWindowTopMost() { ScreenDepth = zDepth.TopMost; InitScreen(); } #endif } }