Unity窗口操作
Unity窗口操作
- unity調(diào)用win32操作窗口
- 1.基本操作
- (1)全屏
- (2)最小化
- (3)除任務(wù)欄全屏
- (4)改變標(biāo)題欄文字
- (5)設(shè)置無邊框,窗口位置及分辨率
- 2.沒有標(biāo)題欄和上邊框時(shí),點(diǎn)動場景拖動窗口
- 3.自由調(diào)整窗口大小
- 4.調(diào)整窗口大小強(qiáng)制固定比例
unity調(diào)用win32操作窗口
1.基本操作
(1)全屏
(2)最小化
(3)除任務(wù)欄全屏
(4)改變標(biāo)題欄文字
(5)設(shè)置無邊框,窗口位置及分辨率
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine;/// <summary> /// 窗口工具系統(tǒng)類(窗口狀態(tài)) /// </summary> public class WindowManager : MonoBehaviour {#region 系統(tǒng)字段 & 系統(tǒng)方法//設(shè)置當(dāng)前窗口的顯示狀態(tài)[DllImport("user32.dll")]public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);//獲取當(dāng)前激活窗口[DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]public static extern System.IntPtr GetForegroundWindow();//獲取指定unity.exe窗口[DllImport("user32.dll", EntryPoint = "FindWindow")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//public static IntPtr ParenthWnd = FindWindow(null, "ProjectName"); //build時(shí)候的項(xiàng)目名,playerSetting中設(shè)置的public static IntPtr ParenthWnd = FindWindow(null, "Win"); //build時(shí)候的項(xiàng)目名,playerSetting中設(shè)置的//設(shè)置窗口邊框[DllImport("user32.dll")]public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);//設(shè)置窗口位置,尺寸[DllImport("user32.dll")]public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);[DllImport("user32.dll", SetLastError = true)]private static extern int GetWindowLong(IntPtr hWnd, int nIndex);//窗口拖動[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport("user32.dll")]public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);//更改標(biāo)題欄[DllImport("user32.dll")]public static extern int SetWindowText(IntPtr hWnd, string text);//使用查找任務(wù)欄[DllImport("user32.dll")]public static extern IntPtr FindWindow(string strClassName, int nptWindowName);//獲取窗口位置以及大小[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);[StructLayout(LayoutKind.Sequential)]public struct RECT{public int Left; //最左坐標(biāo)public int Top; //最上坐標(biāo)public int Right; //最右坐標(biāo)public int Bottom; //最下坐標(biāo)}//邊框參數(shù)private const uint SWP_SHOWWINDOW = 0x0040;private const int GWL_STYLE = -16;private const int WS_BORDER = 1;//隱藏標(biāo)題欄圖標(biāo)private const int WS_POPUP = 0x800000;private const int WS_SYSMENU = 0x80000;//最大最小化private const int SW_SHOWMINIMIZED = 2;//(最小化窗口)private const int SW_SHOWMAXIMIZED = 3;//最大化窗口//去除標(biāo)題欄保留邊框private const int WS_CAPTION = 0x00C00000;private const int WS_THICKFRAME = 0x00040000;#endregion#region 方法/// <summary>/// 最小化窗口/// </summary>public void SetMinWindows(){ShowWindow(ParenthWnd, SW_SHOWMINIMIZED);}private bool taskbarFullScreen = false;//是否是任務(wù)欄全屏// <summary>/// 最大化窗口:窗口全屏/// </summary>public void SetMaxWindows(){if (taskbarFullScreen)SetWindowPos(ParenthWnd, 0, 0, 0, Screen.currentResolution.width, Screen.currentResolution.height + GetTaskBarHeight(), SWP_SHOWWINDOW);elseShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);}/// <summary>/// 除任務(wù)欄外最大化窗口/// </summary>public void TaskbarMaxWindow(){int currMaxScreenHeight = Screen.currentResolution.height - GetTaskBarHeight();SetWindowPos(ParenthWnd, 0, 0, 0, Screen.currentResolution.width, currMaxScreenHeight, SWP_SHOWWINDOW);taskbarFullScreen = !taskbarFullScreen;}/// <summary>/// 設(shè)置無邊框,窗口位置及分辨率/// </summary>/// <param name="rect">尺寸數(shù)據(jù)</param>public void SetNoFrameWindow(Rect rect, bool isMax, bool isDrag = false){if (!isDrag){//去除上邊欄(不可拖拽縮放)SetWindowLong(ParenthWnd, GWL_STYLE, WS_POPUP);}else{if (!isMax){//設(shè)置拖拽縮放模式SetWindowLong(ParenthWnd, GWL_STYLE, GetWindowLong(ParenthWnd, GWL_STYLE)& ~WS_CAPTION | WS_THICKFRAME);}else{//去除上邊欄(不可拖拽縮放)SetWindowLong(ParenthWnd, GWL_STYLE, WS_POPUP);}}//隱藏上邊欄(部分)// SetWindowLong(GetForegroundWindow(), GWL_STYLE, GetWindowLong(GetForegroundWindow(), GWL_STYLE) & ~WS_POPUP);if (isMax){SetMaxWindows();}else{//設(shè)置窗口位置及分辨率bool result = SetWindowPos(ParenthWnd, 0, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, SWP_SHOWWINDOW);}}/// <summary>/// 拖動窗口/// </summary>/// <param name="window">當(dāng)前句柄</param>public void DragWindow(IntPtr window){ReleaseCapture();SendMessage(window, 0xA1, 0x02, 0);SendMessage(window, 0x0202, 0, 0);}public string titleText;/// <summary>/// 改變標(biāo)題欄標(biāo)題/// </summary>public void ChangeTitleText(){SetWindowText(ParenthWnd, titleText);}/// <summary>/// 獲取當(dāng)前窗口尺寸/// </summary>/// <returns></returns>public Rect GetWindowInfo(){RECT rect = new RECT();Rect targetRect = new Rect();GetWindowRect(ParenthWnd, ref rect);targetRect.width = Mathf.Abs(rect.Right - rect.Left);targetRect.height = Mathf.Abs(rect.Top - rect.Bottom);targetRect.x = rect.Left;targetRect.y = rect.Top;return targetRect;}#endregion#region Private Methods/// <summary>/// 獲取任務(wù)欄高度/// </summary>/// <returns>任務(wù)欄高度</returns>private int GetTaskBarHeight(){int taskbarHeight = 10;IntPtr hWnd = FindWindow("Shell_TrayWnd", 0);RECT rect = new RECT();GetWindowRect(hWnd, ref rect);taskbarHeight = rect.Bottom - rect.Top;return taskbarHeight;}#endregion}}2.沒有標(biāo)題欄和上邊框時(shí),點(diǎn)動場景拖動窗口
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine;/// <summary> /// 窗口工具系統(tǒng)類(窗口狀態(tài)) /// </summary> public class WindowsTool : MonoBehaviour { //獲取指定unity.exe窗口[DllImport("user32.dll", EntryPoint = "FindWindow")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//public static IntPtr ParenthWnd = FindWindow(null, "ProjectName"); //build時(shí)候的項(xiàng)目名,playerSetting中設(shè)置的public static IntPtr ParenthWnd = FindWindow(null, "Win"); //build時(shí)候的項(xiàng)目名,playerSetting中設(shè)置的//設(shè)置窗口邊框[DllImport("user32.dll")]public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);//設(shè)置窗口位置,尺寸[DllImport("user32.dll")]public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);[DllImport("user32.dll", SetLastError = true)]private static extern int GetWindowLong(IntPtr hWnd, int nIndex);//窗口拖動[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport("user32.dll")]public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);//邊框參數(shù)private const uint SWP_SHOWWINDOW = 0x0040;private const int GWL_STYLE = -16;private const int WS_BORDER = 1;//隱藏標(biāo)題欄圖標(biāo)private const int WS_POPUP = 0x800000;float xx;bool bx;void Start(){bx = false;xx = 0f; #if UNITY_STANDALONE_WINResolution[] r = Screen.resolutions;Rect screenPosition = new Rect((r[r.Length - 1].width - Screen.width) / 2, (r[r.Length - 1].height - Screen.height) / 2, Screen.width, Screen.height);//去除上邊欄(不可拖拽縮放)SetWindowLong(ParenthWnd, GWL_STYLE, WS_POPUP);//將網(wǎng)上的WS_BORDER替換成WS_POPUP //設(shè)置窗口位置以及分辨率SetWindowPos(ParenthWnd, 0, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); #endif}void Update(){ #if UNITY_STANDALONE_WINif (Input.GetMouseButtonDown(0)){xx = 0f;bx = true;}if (bx && xx >= 0.3f){ //這樣做為了區(qū)分界面上面其它需要滑動的操作ReleaseCapture();SendMessage(ParenthWnd, 0xA1, 0x02, 0);SendMessage(ParenthWnd, 0x0202, 0, 0);}if (bx)xx += Time.deltaTime;if (Input.GetMouseButtonUp(0)){xx = 0f;bx = false;}#endif} }3.自由調(diào)整窗口大小
4.調(diào)整窗口大小強(qiáng)制固定比例
參考網(wǎng)址:
https://blog.csdn.net/u014661152/article/details/113737625?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=2
總結(jié)
- 上一篇: hsv 明度的范围_色彩空间中的 HSL
- 下一篇: Qt弹出的窗口始终位于界面最前面的方法