代码
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class WindowsStyle : MonoBehaviour
{
public Button hideBarBtn;
public Button showBarBtn;
public Button hideCloseBtn;
public Button showCloseBtn;
public Button showMaxSizeBtn;
private void Awake()
{
var hwd = GetForegroundWindow();
hideBarBtn.onClick.AddListener(() =>
{
var wl = GetWindowLong(hwd, GWL_STYLE);
wl &= ~WS_CAPTION;
SetWindowLong(hwd, GWL_STYLE, wl);
});
showBarBtn.onClick.AddListener(() =>
{
var wl = GetWindowLong(hwd, GWL_STYLE);
wl |= WS_CAPTION;
SetWindowLong(hwd, GWL_STYLE, wl);
});
hideCloseBtn.onClick.AddListener(() =>
{
var wl = GetWindowLong(hwd, GWL_STYLE);
wl &= ~WS_SYSMENU;
SetWindowLong(hwd, GWL_STYLE, wl);
});
showCloseBtn.onClick.AddListener(() =>
{
var wl = GetWindowLong(hwd, GWL_STYLE);
wl |= WS_SYSMENU;
SetWindowLong(hwd, GWL_STYLE, wl);
});
showMaxSizeBtn.onClick.AddListener(() =>
{
ShowWindow(hwd, SW_SHOWMAXIMIZED);
});
private void OnApplicationQuit()
{
Application.wantsToQuit += () =>
{
var hwd = GetForegroundWindow();
ShowWindow(hwd, SW_SHOWMINIMIZED);
return false;
};
}
}
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hwd, int cmdShow);
[DllImport("user32.dll")]
public static extern long GetWindowLong(IntPtr hwd, int nIndex);
[DllImport("user32.dll")]
public static extern void SetWindowLong(IntPtr hwd, int nIndex, long dwNewLong);
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOWMAXIMIZED = 3;
const int SW_SHOWRESTORE = 1;
const int GWL_STYLE = -16;
const int WS_CAPTION = 0x00c00000;
const int WS_SYSMENU = 0x00080000;
}
如果是需要尽早执行的话可以推荐将代码放到[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]特性下调用。 RuntimeInitializeOnLoadMethod介绍
|