浅谈Unity之窗口分辨率调整-02

本文涉及的产品
交互式建模 PAI-DSW,每月250计算时 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
模型训练 PAI-DLC,100CU*H 3个月
简介: 窗口分辨率调整

前言:每日记录自己学习unity的心得和体会,小弟才疏学浅,如有错误的地方,欢迎大佬们指正,感谢~

image.png

完整代码

using System;

using System.Collections;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using UnityEngine;

using UnityEngine.UI;

public class ScreenResolution : MonoBehaviour

{

   //窗口样式

   [DllImport("user32.dll")]

   static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

   //窗口分辨率和位置

   [DllImport("user32.dll")]

   static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

   //获取当前激活窗口

   [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]

   static extern IntPtr GetForegroundWindow();

   [DllImport("user32.dll")]

   static extern IntPtr FindWindow(string strClassName, int nptWindowName);

   //窗口拖动

   [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", SetLastError = true)]

   //得到窗口的样式

   private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

   [DllImport("user32.dll")]

   public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);

   //获取窗口位置以及大小

   [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; //最下坐标

   }

   public enum TitleBarStyles

   {

       Hide, Show

   }

   private const int SWP_SHOWWINDOW = 0x0040;

   //边框样式

   private const int GWL_STYLE = -16;

   private const int GWL_EXSTYLE = -20;

   //窗口有细线边框

   private const int WS_BORDER = 1;

   //窗口有一个最大化按钮

   private const int WS_MAXIMIZEBOX = 0x00010000;

   //窗口有一个最小化按钮

   private const int WS_MINIMIZEBOX = 0x00020000;

   //窗口是一个重叠窗口。重叠窗口有标题栏和边框。

   private const int WS_OVERLAPPED = 0x00000000;

   //有标题栏

   private const int WS_TILEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);

   //隐藏标题栏图标

   private const int WS_POPUP = 0x800000;

   //窗口的标题栏上有一个窗口菜单。也必须指定ws_CAPTION样式。

   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;

   /// <summary>

   /// 隐藏标题栏修改分辨率

   /// </summary>

   /// <param name="winWidth"></param>

   /// <param name="winHeight"></param>

   /// <param name="winPosX"></param>

   /// <param name="winPosY"></param>

   void ChangeScreenSizeHide(int winWidth, int winHeight, int winPosX, int winPosY)

   {

       if (titleBarStyles== TitleBarStyles.Show)

       {

           //去除标题栏(不可拖拽缩放);一定要加SetWindowPos()设置才会立即生效

           SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);

           //去除标题栏(可拖拽缩放)

           //SetWindowLong(GetForegroundWindow(), GWL_STYLE, GetWindowLong(GetForegroundWindow(), GWL_STYLE) & ~WS_CAPTION | WS_THICKFRAME);

       }

       ChangeScreenSize(winWidth, winHeight, winPosX, winPosY);

       titleBarStyles = TitleBarStyles.Hide;

   }

   //不隐藏标题栏修改分辨率

   void ChangeScreenSizeShow(int winWidth, int winHeight, int winPosX, int winPosY)

   {

       if (titleBarStyles == TitleBarStyles.Hide)

       {

           SetWindowLong(GetForegroundWindow(), GWL_STYLE, GetWindowLong(GetForegroundWindow(), GWL_STYLE) | WS_TILEDWINDOW);

       }

       ChangeScreenSize(winWidth, winHeight, winPosX, winPosY);

       titleBarStyles = TitleBarStyles.Show;

   }

   /// <summary>

   /// 修改分辨率

   /// </summary>

   /// <param name="winWidth"></param>

   /// <param name="winHeight"></param>

   /// <param name="winPosX"></param>

   /// <param name="winPosY"></param>

   /// <param name="winPoscy"></param>

   void ChangeScreenSize(int winWidth, int winHeight, int winPosX, int winPosY)

   {

       SetWindowPos(GetForegroundWindow(), 0, winPosX - (int)(winWidth * 0.5f), winPosY - (int)(winHeight * 0.5f), winWidth, winHeight, SWP_SHOWWINDOW);

   }

   void Delay(float seconds, Action onFinished)

   {

       StartCoroutine(DelayCoroutione(seconds, onFinished));

   }

   private IEnumerator DelayCoroutione(float seconds, Action onFinished)

   {

       yield return new WaitForSeconds(seconds);

       onFinished();

   }

   /// <summary>

   /// 窗口最大化

   /// </summary>

   public void SetMaxWindows()

   {

       int currMaxScreenHeight = Screen.currentResolution.height - GetTaskBarHeight();

       ChangeScreenSizeHide(Screen.currentResolution.width, currMaxScreenHeight,  (int)(Screen.currentResolution.width * 0.5f), (int)(currMaxScreenHeight * 0.5f));

      // SetWindowPos(GetForegroundWindow(), 0, 0, 0, Screen.currentResolution.width, currMaxScreenHeight, SWP_SHOWWINDOW);

   }

   /// <summary>

   /// 获取任务栏高度

   /// </summary>

   /// <returns>任务栏高度</returns>

   private int GetTaskBarHeight()

   {

       int taskbarHeight = 0;

       IntPtr hWnd = FindWindow("Shell_TrayWnd", 0);

       RECT rect = new RECT();

       GetWindowRect(hWnd, ref rect);

       taskbarHeight = (int)rect.Bottom - (int)rect.Top;

       Debug.Log(taskbarHeight);

       return taskbarHeight;

   }

   /// <summary>

   /// 拖动窗口

   /// </summary>

   /// <param name="window">当前句柄</param>

   public void DragWindow()

   {

       ReleaseCapture();

       SendMessage(GetForegroundWindow(), 0xA1, 0x02, 0);

       SendMessage(GetForegroundWindow(), 0x0202, 0, 0);

   }

   /// <summary>

   /// 最小化窗口

   /// </summary>

   public void SetMinWindows()

   {

       ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);

   }

   /// <summary>

   /// 全屏

   /// </summary>

   public void SetFullScreen()

   {

       ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);

   }

}

目录
打赏
0
0
0
0
3
分享
相关文章
Unity3D初识---窗口和菜单基础介绍
目录 目录 窗口界面介绍: scene: Game : Hierarchy: Project: Inspector: 其他窗口: 菜单界面 File文件菜单 Edit编辑菜单 窗口界面介绍: scene: 在场景编译窗口中可以鼠标右键选中之后移动鼠标就可以自由的浏览场景,并选择物体对其可以进行操作,按下Q\W\E\R四个键时可以画面中的将会进行变换,第一个图标可以进行拖动、第二个图标可以对物体进行位移,第三个图标可以对物体进行旋转,第四个图标可以对物体进行放大缩小。
1315 0
unity3d实现Game窗口的Stats
示例.png using UnityEngine; using System.Text; using UnityEditor; public class ShowFps : MonoBehaviour { public bool...
1158 0
unity3d UGUI的down与up弹窗,松开时关闭窗口处理机制
挂载到对象即可,然后注册回调做down和up的处理 using UnityEngine; using UnityEngine.EventSystems; public class PushListener : MonoBehaviour,IPointe...
826 0
unity Hierarchy窗口对象的显示和隐藏与锁定
setting.png 将脚本QuickToggle放置到Editor目标下 /* Copyright 2017, Jeiel Aranal Permission is hereby granted, free of charge, t...
2639 0
Unity 3D调用Windows打开、保存窗口、文件浏览器
Unity调用Window窗口 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar ——...
3366 0
unity 实现调用Windows窗口/对话框交互
Unity调用Window窗口 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar ——...
1869 0
Unity用GUI绘制Debug/print窗口/控制台-打包后测试
Unity游戏视窗控制台输出 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar —— 心...
2121 0
掌握Unity调试与测试的终极指南:从内置调试工具到自动化测试框架,全方位保障游戏品质不踩坑,打造流畅游戏体验的必备技能大揭秘!
【9月更文挑战第1天】在开发游戏时,Unity 引擎让创意变为现实。但软件开发中难免遇到 Bug,若不解决,将严重影响用户体验。调试与测试成为确保游戏质量的最后一道防线。本文介绍如何利用 Unity 的调试工具高效排查问题,并通过 Profiler 分析性能瓶颈。此外,Unity Test Framework 支持自动化测试,提高开发效率。结合单元测试与集成测试,确保游戏逻辑正确无误。对于在线游戏,还需进行压力测试以验证服务器稳定性。总之,调试与测试贯穿游戏开发全流程,确保最终作品既好玩又稳定。
274 4

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等