ict.shenzhi/Assets/Scripts/WindowControl/WindowManager.cs

250 lines
7.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
/// <summary>
/// 程序窗口管理
/// </summary>
public class WindowManager : MonoBehaviour
{
public static WindowManager Instance;
public WindowsToolMgr tool_mgr;
public GameObject main;
public GameObject mini;
public enum ScreenResolution
{
/// <summary>
/// 主窗口 1600*900
/// </summary>
MAX_SCREEN,
/// <summary>
/// 小窗+面板 300*800
/// </summary>
MID_SCREEN,
/// <summary>
/// 小窗 300*170
/// </summary>
MINI_SCREEN,
/// <summary>
/// 长窗口
/// </summary>
LONG_SCREEN
}
/// <summary>
/// 窗口模式-窗口尺寸对照
/// </summary>
private Dictionary<ScreenResolution, int[]> resolution_size = new Dictionary<ScreenResolution, int[]>() {
{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}},
};
/// <summary>
/// 屏幕模式
/// </summary>
public static ScreenResolution screen_resolution = ScreenResolution.MAX_SCREEN;
/// <summary>
/// 是否为主窗口模式
/// </summary>
public static bool max_screen => screen_resolution == ScreenResolution.MAX_SCREEN;
/// <summary>
/// 是否退出程序
/// </summary>
public static bool quit;
/// <summary>
/// 窗口坐标
/// </summary>
private ResolutionTest.RECT screen_position;
/// <summary>
/// 正在刷新分辨率
/// </summary>
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
}
/// <summary>
/// 切换分辨率
/// </summary>
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;
/// <summary>
/// 置顶设置
/// </summary>
public void Topping(bool _is_topping)
{
is_topping = _is_topping;
tool_mgr.SetTopping(_is_topping);
}
/// <summary>
/// 不改变模式,设置分辨率
/// </summary>
public void SetResolution(int _width, int _height)
{
tool_mgr.SwitchScreenResolution(_width, _height);
}
}