当前位置:   article > 正文

【Unity】Windows无边框窗口_unity 无边框窗口化

unity 无边框窗口化

无边框实现脚本

目前网上大多数无边框实现的脚本,都是使用GetForegroundWindow()获取窗体句柄,如此便导致了程序启动时不能点击其他窗口,否则被点击到的其他窗体将会被无边框。

修改后,使用Process仅获取当前程序窗体句柄,避免以上问题

        //Handle = GetForegroundWindow();
        //使用Process.GetProcessById(currentProcessId)代替GetForegroundWindow(),避免其他被点击的窗体无边框
        // 获取当前 Unity 应用程序的进程ID
        int currentProcessId = Process.GetCurrentProcess().Id;
        // 根据进程ID获取进程对象
        Process currentProcess = Process.GetProcessById(currentProcessId);
        // 获取当前进程的主窗口句柄
        Handle = currentProcess.MainWindowHandle;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

完整代码:

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

public class BorderlessWindow : MonoBehaviour
{
    public Rect screenPosition;
    [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")]
    static extern IntPtr GetForegroundWindow();//获取当前获取了焦点的窗口句柄

    [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 bool ShowWindow(IntPtr hwnd, int nCmdShow);

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    // GetSystemMetrics实际获取的是系统记录的分辨率,不是物理分辨率,如屏幕2560*1600,显示缩放200%,这里获取到的是1280*800
    [DllImport("user32.dll", SetLastError = true)]

   

    private static extern int GetSystemMetrics(int nIndex);
    private static int SM_CXSCREEN = 0; //主屏幕分辨率宽度
    private static int SM_CYSCREEN = 1; //主屏幕分辨率高度
    private static int SM_CYCAPTION = 4; //标题栏高度
    private static int SM_CXFULLSCREEN = 16; //最大化窗口宽度(减去任务栏)
    private static int SM_CYFULLSCREEN = 17; //最大化窗口高度(减去任务栏)

    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    const int WS_POPUP = 0x800000;
    const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
    const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活} 
    const int SW_SHOWRESTORE = 1;//还原

    IntPtr Handle;
    float xx;
    bool bx;

    private const int initWidth = 1920;
    private const int initHeight = 1080;



    int newWidth;
    int newHeight;


    public GameObject maxButton;
    public GameObject midButton;


    private void Start()
    {
        bx = false;
        xx = 0f;

        // 屏幕分辨率
        newWidth = GetSystemMetrics(SM_CXSCREEN);
        newHeight = GetSystemMetrics(SM_CYSCREEN);


        float offsetWidth = (float)newWidth / (float)initWidth;
        float offsetHeight = (float)newHeight / (float)initHeight;


        float offset = (offsetWidth + offsetHeight) * 0.4f;

        newWidth = (int)(initWidth * offset);
        newHeight = (int)(initHeight * offset);

        // 获取当前 Unity 应用程序的进程ID
        int currentProcessId = Process.GetCurrentProcess().Id;
        // 根据进程ID获取进程对象
        Process currentProcess = Process.GetProcessById(currentProcessId);
        // 获取当前进程的主窗口句柄
        Handle = currentProcess.MainWindowHandle;

        Borderless();
    }



    void Update()
    {
#if UNITY_STANDALONE_WIN
        //当点击到名为"Frame"的UI时,方可拖动窗体
        if (IsPointerOverGameObject("Frame"))
        {
            if (Input.GetMouseButtonDown(0))
            {
                xx = 0f;
                bx = true;
            }
            if (bx && xx >= 0)
            { //这样做为了区分界面上面其它需要滑动的操作
                ReleaseCapture();
                SendMessage(Handle, 0xA1, 0x02, 0);
                SendMessage(Handle, 0x0202, 0, 0);
            }
            if (bx)
                xx += Time.deltaTime;
            if (Input.GetMouseButtonUp(0))
            {
                xx = 0f;
                bx = false;
            }
        }

#endif
    }


    /// <summary>
    /// 实现无边框
    /// </summary>
    private void Borderless()
    {
#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, Screen.width, Screen.height);
        SetWindowLong(Handle, GWL_STYLE, WS_POPUP);//将网上的WS_BORDER替换成WS_POPUP  
        SetWindowPos(Handle, 0, (int)screenPosition.x, (int)screenPosition.y, initWidth, initHeight, SWP_SHOWWINDOW);
#endif
    }


    public bool IsPointerOverGameObject(string name)
    {
        bool hasItemMenu = false;
        //实例化点击事件
        PointerEventData eventDataCurrentPosition = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
        //将点击位置的屏幕坐标赋值给点击事件
        //eventDataCurrentPosition.position = new Vector2(screenPosition.x, screenPosition.y);
        eventDataCurrentPosition.position = Input.mousePosition;
        List<RaycastResult> results = new List<RaycastResult>();
        //向点击处发射射线
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

        for (int i = 0; i < results.Count; i++)
        {
            if (results[i].gameObject.name == name)
            {
                hasItemMenu = true;
            }
        }
        return hasItemMenu;
    }

    /// <summary>
    /// 最小化窗口
    /// </summary>
    public void btn_MinClick()
    { 
        ShowWindow(Handle, SW_SHOWMINIMIZED);
    }

    /// <summary>
    /// 最大化窗口
    /// </summary>
    public void btn_MaxClick()
    {
        ShowWindow(Handle, SW_SHOWMAXIMIZED);
    }

    /// <summary>
    /// 还原窗口
    /// </summary>
    public void Btn_InitWindowsClick()
    {
        ShowWindow(Handle, SW_SHOWRESTORE);
    }

}

  • 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
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/783236
推荐阅读
相关标签
  

闽ICP备14008679号