Unity打包窗口化放大、缩小、拖拽功能、无边框设置 C#

Unity打包Windows窗口实现放大、缩小、拖拽、无边框


文章目录

  • Unity打包Windows窗口实现放大、缩小、拖拽、无边框
  • 前言
  • 一、引入 user32.dll
  • 二、使用步骤
    • 1.引入库
    • 2.功能封装
    • 3.效果图如下,绑定自定义按钮
  • 总结


前言

Unity无边框设置、窗口化放大、缩小、拖拽
提示:PC端打包,测试尽量在打包后测试。
编辑器下测试会有意想不到的后果呦~~


一、引入 user32.dll

[DllImport("user32.dll")]

二、使用步骤

1.引入库

代码如下:

        [DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);[DllImport("user32.dll")]private static extern IntPtr GetForegroundWindow();[DllImport("user32.dll")]private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);[DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy,uint uFlags);[DllImport("user32.dll")]private static extern bool ReleaseCapture();[DllImport("user32.dll")]private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

2.功能封装

代码如下:注释详细,不懂就问哈

 public class WindowSetting : MonoBehaviour{[DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);[DllImport("user32.dll")]private static extern IntPtr GetForegroundWindow();[DllImport("user32.dll")]private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);[DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy,uint uFlags);[DllImport("user32.dll")]private static extern bool ReleaseCapture();[DllImport("user32.dll")]private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}const int SW_SHOWMAXIMIZED = 3; //最大化const int SW_SHOWRESTORE = 1; //还原const uint SWP_SHOWWINDOW = 0x0040;const int GWL_STYLE = -16;const int WS_POPUP = 0x800000;private Rect _screenPosition;private IntPtr _handle;/// <summary>/// 放大全屏按钮  缩小按钮  还原窗口按钮  退出按钮/// </summary>public Button bigBtn, smallBtn, restoreBtn, closeBtn;/// <summary>/// 自定义屏幕分辨率 长高/// </summary>private int _screenWidth, _screenHeight;private void Start(){bigBtn.onClick.AddListener(OnClickMaximize);smallBtn.onClick.AddListener(OnClickMinimize);restoreBtn.onClick.AddListener(OnClickRestore);closeBtn.onClick.AddListener(CloseWindow);//自定义窗口分辨率_screenWidth = 1280;_screenHeight = 720;#if UNITY_STANDALONE_WIN && !UNITY_EDITOR //作用是编辑器模式下不运行 尽量打包运行测试Resolution[] r = Screen.resolutions;_screenPosition =new Rect((r[r.Length - 1].width - Screen.width) / 2, (r[r.Length - 1].height - Screen.height) / 2, _screenWidth, _screenHeight);SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);//将网上的WS_BORDER替换成WS_POPUP  _handle = GetForegroundWindow();   //FindWindow ((string)null, "popu_windows");SetWindowPos(GetForegroundWindow(), 0, (int)_screenPosition.x, (int)_screenPosition.y, (int)_screenPosition.width, (int)_screenPosition.height, SWP_SHOWWINDOW);
#endif}/// <summary>/// 最小化 隐藏/// </summary>private static void OnClickMinimize(){ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);}/// <summary>/// 最大化 全屏/// </summary>private static void OnClickMaximize(){ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);}/// <summary>/// 还原窗口/// </summary>private static void OnClickRestore(){ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);}/// <summary>/// 退出/// </summary>private void CloseWindow(){Application.Quit();}/// <summary>/// 窗口拖拽区域  此处调用可以在按钮上添加EventTrigger组件 使用Drag方法/// </summary>public void DragWindowsMethod(){ReleaseCapture();SendMessage(_handle, 0xA1, 0x02, 0);SendMessage(_handle, 0x0202, 0, 0);}}

3.效果图如下,绑定自定义按钮

在这里插入图片描述

总结

代码整体已贴出,功能亲测可用,欢迎学习交流。点赞+收藏+关注哦~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/29587.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

# Linux下替换删除文件中的颜色等控制字符的方法

Linux下替换删除文件中的颜色等控制字符的方法 文章目录 Linux下替换删除文件中的颜色等控制字符的方法1 Linux下的控制字符&#xff08;显示的文字并不是他本身&#xff09;&#xff1a;2 颜色字符范例&#xff1a;3 替换4 最后 我们在shell编程显示输出时&#xff0c;会定义文…

小白带你学习Linux的rsync的基本操作(二十四)

目录 前言 一、概述 二、特性 1、快速 2、安全 三、应用场景 四、数据的同步方式 五、rsync传输模式 六、rsync应用 七、rsync命令 1、格式 2、选项 3、举例 4、配置文件 5、练习 八、rsyncinotfy实时同步 1、服务器端 2、开发客户端 前言 Rsync是一个开源的…

Linux 安装oracle_11g保姆级安装教程及安装Oracle常见问题处理

一 安装linux 1 Linux系统 安装前的准备 linux安装建议&#xff1a; 1&#xff09;脑内存1G&#xff0c;交换空间是物理内存的两倍&#xff0c;空闲空间&#xff08;/tmp目录&#xff09;至少1G 2&#xff09;选择服务时&#xff0c;建议全部选中&#xff0c;关闭防火墙&am…

python实现正态分布变换NDT算法(notebook版本)

直接看notebook&#xff0c;写的应该很清楚了 准备数据 假设两帧机器人相对位置变化如下 采集得到两帧点云 构建得分地图 优化点云位姿 红色为初值&#xff0c;蓝色为优化结果&#xff0c;可以看出NDT算法对初值要求不高。

标注工具Labelimg,正常运行显示,但是对图片点击Create RectBox画矩形框开始闪退

问题描述*&#xff1a;标注工具Labelimg&#xff0c;正常运行显示&#xff0c;但是对图片点击Create RectBox画矩形框开始闪退&#xff0c;闪退出现以下代码 File “C:\ProgramData\anaconda3\Lib\site-packages\libs\canvas.py”, line 530, in paintEvent p.drawLine(self.p…

图采样、随机游走、subgraph的实现

诸神缄默不语-个人CSDN博文目录 本博文主要关注如何用代码实现图采样、随机游走、subgraph&#xff08;为什么这些东西放在一起写&#xff0c;我感觉还蛮直觉的&#xff09;。 随机游走和subgraph我之前都写过不少博文了&#xff0c;可以参考↑ 这个主要是我前年还在干GNN时候…

Hugging Face开源库accelerate详解

官网&#xff1a;https://huggingface.co/docs/accelerate/package_reference/accelerator Accelerate使用步骤 初始化accelerate对象accelerator Accelerator()调用prepare方法对model、dataloader、optimizer、lr_schedluer进行预处理删除掉代码中关于gpu的操作&#xff0…

Windows11 C盘瘦身

1.符号链接 将大文件夹移动到其他盘&#xff0c;创建成符号链接 2.修改Android Studio路径设置 1.SDK路径 2.Gradle路径 3.模拟器路径 设置环境变量 ANDROID_SDK_HOME

五、DQL-1.概述

一、DQL介绍 Data Query Language 数据查询语言 用来查询数据库中表的记录。 查询关键字&#xff1a;SELECT 二、语法

每日一题2023.7.19|ACM模式

文章目录 C的输入方式介绍cin>>cin.get(字符变量名)cin.get(数组名,接收字符数目)cin.get()cin.getline() getline()gets()getchar() AB问题|AB问题||AB问题|||ABⅣAB问题ⅤAB问题Ⅵ C的输入方式介绍 参考博客 cin>> 最基本&#xff0c;最常用的字符或者数字的输…

Ceph 分布式存储之部署

一.Ceph 存储基础 1、单机存储设备 DAS&#xff08;直接附加存储&#xff0c;是直接接到计算机的主板总线上去的存储&#xff09; IDE、SATA、SCSI、SAS、USB 接口的磁盘 所谓接口就是一种存储设备驱动下的磁盘设备&#xff0c;提供块级别的存储 NAS&#xff08;网络附加存储…

ES系列--文档处理

一、文档冲突 当我们使用 index API 更新文档 &#xff0c;可以一次性读取原始文档&#xff0c;做我们的修改&#xff0c;然后重 新索引 整个文档 。 最近的索引请求将获胜&#xff1a;无论最后哪一个文档被索引&#xff0c;都将被唯一存 储在 Elasticsearch 中。如果其他人同时…