当前位置:   article > 正文

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

unity 可改变窗口大小

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



前言

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

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

在这里插入图片描述

总结

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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号