using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; /// /// 程序窗口管理 /// public class WindowManager : MonoBehaviour { public static WindowManager Instance; public WindowsToolMgr tool_mgr; public GameObject main; public GameObject mini; public enum ScreenResolution { /// /// 主窗口 1600*900 /// MAX_SCREEN, /// /// 小窗+面板 300*800 /// MID_SCREEN, /// /// 小窗 300*170 /// MINI_SCREEN, /// /// 长窗口 /// LONG_SCREEN } /// /// 窗口模式-窗口尺寸对照 /// private Dictionary resolution_size = new Dictionary() { {ScreenResolution.MINI_SCREEN,new int[2]{ 299,203}}, {ScreenResolution.MID_SCREEN,new int[2]{ 299,595}}, {ScreenResolution.LONG_SCREEN,new int[2]{ 299,874}}, {ScreenResolution.MAX_SCREEN,new int[2]{ 1600,900}}, }; /// /// 屏幕模式 /// public static ScreenResolution screen_resolution = ScreenResolution.MAX_SCREEN; /// /// 是否为主窗口模式 /// public static bool max_screen => screen_resolution == ScreenResolution.MAX_SCREEN; /// /// 是否退出程序 /// public static bool quit; /// /// 窗口坐标 /// private ResolutionTest.RECT screen_position; /// /// 正在刷新分辨率 /// private bool is_resolution_refreshing; private void Awake() { Instance = this; //退出确认 (包括右上角×、alt+f4) Application.wantsToQuit += () => { SwitchResolution(ScreenResolution.MAX_SCREEN); if (!quit) { Debug.Log(MainCanvasManager.confirm_panel.name); MainCanvasManager.confirm_panel.OnPopup("即将退出", "是否确认退出?", "提示", (_check) => { if (_check) { quit = true; Application.Quit(); } }); } return quit; }; } // 测试字段 public string wind; public int mode; public Rect rect; [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); private void Update() { if (!is_resolution_refreshing) screen_position = tool_mgr.GetRect(); #if UNITY_EDITOR if (Input.GetKeyDown(KeyCode.Keypad1)) screen_resolution = ScreenResolution.MAX_SCREEN; if (Input.GetKeyDown(KeyCode.Keypad2)) screen_resolution = ScreenResolution.MINI_SCREEN; if (Input.GetKeyDown(KeyCode.Keypad3)) { IntPtr wi = FindWindow(null, wind); tool_mgr.winTool.SetTopMost(wi, mode, rect); } if (Input.GetKeyDown(KeyCode.Keypad4)) { IntPtr wi = FindWindow(null, wind); tool_mgr.winTool.SetFrameWindow(wi, rect); } if (Input.GetKeyDown(KeyCode.Keypad5)) { IntPtr wi = FindWindow(null, wind); tool_mgr.winTool.SetNoFrameWindow(wi, rect); } if (Input.GetKeyDown(KeyCode.Keypad6)) { IntPtr wi = FindWindow(null, wind); var rect = tool_mgr.winTool.GetWinRect(wi); Debug.Log(rect.Left + "-" + rect.Top + "-" + (rect.Right - rect.Left) + "-" + (rect.Bottom - rect.Top)); } if (Input.GetKeyDown(KeyCode.Keypad7)) { IntPtr wi = FindWindow(null, wind); var rect = tool_mgr.winTool.GetWindowLongA(wi, mode); Debug.Log(rect); } #endif } /// /// 切换分辨率 /// public void SwitchResolution(ScreenResolution _screen_resolution) { #if !UNITY_EDITOR if (screen_resolution != _screen_resolution) { screen_resolution = _screen_resolution; is_resolution_refreshing = true; //tool_mgr.SwitchScreenResolution(resolution_size[_screen_resolution]); if (max_screen) tool_mgr.SetFrame(resolution_size[_screen_resolution][0], resolution_size[_screen_resolution][1]+30, screen_position.Left, screen_position.Top); else tool_mgr.SetNoFrame(resolution_size[_screen_resolution][0], resolution_size[_screen_resolution][1], screen_position.Left, screen_position.Top); Invoke("EndRefreshing", 0.1f); switch (_screen_resolution) { case ScreenResolution.MAX_SCREEN: //tool_mgr.SwitchScreenResolution(1600, 900); mini.SetActive(false); main.SetActive(true); break; case ScreenResolution.MID_SCREEN: //tool_mgr.SwitchScreenResolution(299, 461); mini.SetActive(true); main.SetActive(false); break; case ScreenResolution.MINI_SCREEN: //tool_mgr.SwitchScreenResolution(299, 203); mini.SetActive(true); main.SetActive(false); break; default: break; } } #endif //if (screen_resolution != _screen_resolution) //{ // screen_resolution = _screen_resolution; // is_resolution_refreshing = true; // //tool_mgr.SwitchScreenResolution(resolution_size[_screen_resolution]); // if (max_screen) // tool_mgr.SetFrame(resolution_size[_screen_resolution][0], resolution_size[_screen_resolution][1] + 30, // screen_position.Left, screen_position.Top); // else // tool_mgr.SetNoFrame(resolution_size[_screen_resolution][0], resolution_size[_screen_resolution][1], // screen_position.Left, screen_position.Top); // Invoke("EndRefreshing", 0.1f); // switch (_screen_resolution) // { // case ScreenResolution.MAX_SCREEN: // //tool_mgr.SwitchScreenResolution(1600, 900); // mini.SetActive(false); // main.SetActive(true); // break; // case ScreenResolution.MID_SCREEN: // //tool_mgr.SwitchScreenResolution(299, 461); // mini.SetActive(true); // main.SetActive(false); // break; // case ScreenResolution.MINI_SCREEN: // //tool_mgr.SwitchScreenResolution(299, 203); // mini.SetActive(true); // main.SetActive(false); // break; // default: // break; // } //} } private void EndRefreshing() { is_resolution_refreshing = false; } public void MinWindow() { tool_mgr.MinWindows(); } public bool is_topping; /// /// 置顶设置 /// public void Topping(bool _is_topping) { is_topping = _is_topping; tool_mgr.SetTopping(_is_topping); } /// /// 不改变模式,设置分辨率 /// public void SetResolution(int _width, int _height) { tool_mgr.SwitchScreenResolution(_width, _height); } }