Unity发布PC 设置窗口 无边框(显示win任务栏),或全屏无边框

设置win窗口无边框,就需要对获得Win的窗口信息,所以需要调用Win32的API [DllImport(“user32.dll”)]

Win32 API

//并不是很全,如果在此没找到想要的函数,可点击Win32 API查找
c#调用Win32详解

//下面脚本中 可以设置 除任务栏外无边框,也可以设置全屏无边框

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class MyWindow : MonoBehaviour
{

    //使用查找任务栏
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string strClassName, int nptWindowName);
    //当前窗口
    [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
    //获取窗口位置以及大小
    [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left; //最左坐标
        public int Top; //最上坐标
        public int Right; //最右坐标
        public int Bottom; //最下坐标
    }
    //设置窗口位置,尺寸
    [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    //设置无windows自带边框
    [DllImport("user32.dll")] static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;

    Resolution[] resolutions;//分辨率
    private Rect screenPosition;//最终的屏幕的位置和长宽



    void Awake()
    {
        // Cursor.visible = false; // 鼠标隐藏
        //获取当前屏幕分辩率  
        resolutions = Screen.resolutions;
        //除任务栏外最大化窗口
        witnOutBorder();
        //设置全屏无边框
        //Setposition();
    }
    /// <summary>
    /// 获取当前窗口尺寸
    /// </summary>
    /// <returns></returns>
    public Rect GetWindowInfo()
    {
        RECT rect = new RECT();
        Rect targetRect = new Rect();
        GetWindowRect(GetForegroundWindow(), 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;
    }
    /// <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 = (int)(rect.Bottom - rect.Top);      //得到任务栏的高度
        return taskbarHeight;
    }
    /// <summary>
    /// 除任务栏外最大化窗口(无边框)
    /// </summary>
    private void witnOutBorder()
    {
        //新的屏幕宽度
        screenPosition.width = resolutions[resolutions.Length - 1].width;
        //新的屏幕高度=当前屏幕分辨率的高度-状态栏的高度
        int currMaxScreenHeight = Screen.currentResolution.height - GetTaskBarHeight();
        screenPosition.height = currMaxScreenHeight;
        //新的分辨率(exe文件新的宽高)  这个是Unity里的设置屏幕大小,
        Screen.SetResolution((int)screenPosition.width, (int)screenPosition.height, false);

        //screenPosition.x = (int)((Screen.currentResolution.width - screenPosition.width) / 2);//宽度居中
        //screenPosition.y = (int)((Screen.currentResolution.height - screenPosition.height) / 2);//高度居中

        //设置无框
        SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);
        //exe居左上显示;
       bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
        //exe居中显示;
       // bool result = SetWindowPos(GetForegroundWindow(), 0, (int)screenPosition.x, (int)screenPosition.y,  (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
    }

    /// <summary>
    /// 设置全屏无边框
    /// </summary>
    private void Setposition()
    {
        SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);      //无边框
        bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, resolutions[resolutions.Length - 1].width, resolutions[resolutions.Length - 1].height, SWP_SHOWWINDOW);
    }
}

运行第一次 界面还是会出现边框,需要在打包时再设置一下,
在这里插入图片描述
在这里插入图片描述

参考无边框设置链接

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值