Unity窗口操作

Unity窗口操作

unity调用win32操作窗口

1.基本操作

(1)全屏

(2)最小化

(3)除任务栏全屏

(4)改变标题栏文字

(5)设置无边框,窗口位置及分辨率

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

/// <summary>
/// 窗口工具系统类(窗口状态)
/// </summary>
public class WindowManager : MonoBehaviour
{ 
    #region 系统字段 & 系统方法

    //设置当前窗口的显示状态
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);

    //获取当前激活窗口
    [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时候的项目名,playerSetting中设置的
    public static IntPtr ParenthWnd = FindWindow(null, "Win");    //build时候的项目名,playerSetting中设置的

    //设置窗口边框
    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

    //设置窗口位置,尺寸
    [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);

    //更改标题栏
    [DllImport("user32.dll")]
    public static extern int SetWindowText(IntPtr hWnd, string text);

    //使用查找任务栏
    [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; //最左坐标
        public int Top; //最上坐标
        public int Right; //最右坐标
        public int Bottom; //最下坐标
    }

    //边框参数
    private const uint SWP_SHOWWINDOW = 0x0040;
    private const int GWL_STYLE = -16;
    private const int WS_BORDER = 1;

    //隐藏标题栏图标
    private const int WS_POPUP = 0x800000;
    private const int WS_SYSMENU = 0x80000;

    //最大最小化
    private const int SW_SHOWMINIMIZED = 2;//(最小化窗口)
    private const int SW_SHOWMAXIMIZED = 3;//最大化窗口

    //去除标题栏保留边框
    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;//是否是任务栏全屏
    // <summary>
    /// 最大化窗口:窗口全屏
    /// </summary>
    public void SetMaxWindows()
    { 
        if (taskbarFullScreen)
            SetWindowPos(ParenthWnd, 0, 0, 0, Screen.currentResolution.width, Screen.currentResolution.height + GetTaskBarHeight(), SWP_SHOWWINDOW);
        else
            ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
    }

    
    /// <summary>
    /// 除任务栏外最大化窗口
    /// </summary>
    public void TaskbarMaxWindow()
    { 
        int currMaxScreenHeight = Screen.currentResolution.height - GetTaskBarHeight();
        SetWindowPos(ParenthWnd, 0, 0, 0, Screen.currentResolution.width, currMaxScreenHeight, SWP_SHOWWINDOW);
        taskbarFullScreen = !taskbarFullScreen;
        
    }


    /// <summary>
    /// 设置无边框,窗口位置及分辨率
    /// </summary>
    /// <param name="rect">尺寸数据</param>
    public void SetNoFrameWindow(Rect rect, bool isMax, bool isDrag = false)
    { 
        if (!isDrag)
        { 
            //去除上边栏(不可拖拽缩放)
            SetWindowLong(ParenthWnd, GWL_STYLE, WS_POPUP);
        }
        else
        { 
            if (!isMax)
            { 
                //设置拖拽缩放模式
                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
        { 
            //设置窗口位置及分辨率
            bool result = SetWindowPos(ParenthWnd, 0, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, SWP_SHOWWINDOW);
        }
    }

    /// <summary>
    /// 拖动窗口
    /// </summary>
    /// <param name="window">当前句柄</param>
    public void DragWindow(IntPtr window)
    { 
        ReleaseCapture();
        SendMessage(window, 0xA1, 0x02, 0);
        SendMessage(window, 0x0202, 0, 0);
    }


    public string titleText;
    /// <summary>
    /// 改变标题栏标题
    /// </summary>
    public void ChangeTitleText()
    { 
        SetWindowText(ParenthWnd, titleText);
    }

    /// <summary>
    /// 获取当前窗口尺寸
    /// </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>
    /// 获取任务栏高度
    /// </summary>
    /// <returns>任务栏高度</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.没有标题栏和上边框时,点动场景拖动窗口

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

/// <summary>
/// 窗口工具系统类(窗口状态)
/// </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时候的项目名,playerSetting中设置的
    public static IntPtr ParenthWnd = FindWindow(null, "Win");    //build时候的项目名,playerSetting中设置的

    //设置窗口边框
    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

    //设置窗口位置,尺寸
    [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);


    //边框参数
    private const uint SWP_SHOWWINDOW = 0x0040;
    private const int GWL_STYLE = -16;
    private const int WS_BORDER = 1;

    //隐藏标题栏图标
    private const int WS_POPUP = 0x800000;

    float xx;
    bool bx;
    void Start()
    { 
        bx = false;
        xx = 0f;
#if UNITY_STANDALONE_WIN
        Resolution[] 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);//将网上的WS_BORDER替换成WS_POPUP 
        //设置窗口位置以及分辨率
        SetWindowPos(ParenthWnd, 0, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
#endif
    }


    void Update()
    { 
#if UNITY_STANDALONE_WIN
        if (Input.GetMouseButtonDown(0))
        { 

            xx = 0f;
            bx = true;
        }
        if (bx && xx >= 0.3f)
        {  //这样做为了区分界面上面其它需要滑动的操作
            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.自由调整窗口大小

《Unity窗口操作》

4.调整窗口大小强制固定比例

参考网址:
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

    原文作者:Raki_0
    原文地址: https://blog.csdn.net/weixin_45686837/article/details/123527785
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞