A7:Unity窗口化无边框模式

将下面代码复制进入脚本里,然后随便放到一个物体上即可实现窗口化启动无边框哦~

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
public class WindowMod : MonoBehaviour
{
///
/// 窗口宽度
///
private int winWidth;
///
/// 窗口高度
///
private int winHeight;
///
/// 窗口左上角x
///
private int winPosX;
///
/// 窗口左上角y
///
private int winPosY;

[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();

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

///
/// 移除EXE的边框
///
void MoveEXEBound()
{
winWidth = 1280;
winHeight = 720;
//显示器支持的所有分辨率
int i = Screen.resolutions.Length;

  int resWidth = Screen.resolutions[i - 1].width;
  int resHeight = Screen.resolutions[i - 1].height;

  winPosX = resWidth / 2 - winWidth / 2;
  winPosY = resHeight / 2 - winHeight / 2;

  SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
  bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);

}
}

第二种方法: 动态设置有/无边框,最大化、最小化、鼠标拖动窗口

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

public class WindowsSetting : SingletonBase
{

//系统方法类实体
public WindowsTools winTool = new WindowsTools();

//当前Unity程序进程
private static IntPtr currentWindow;

public void Start()
{
currentWindow = WindowsTools.GetForegroundWindow();
}

//最小化窗口
public void MinWindows()
{
    winTool.SetMinWindows();

}

//设置无边框窗口
public void SetNoFrame()
{
    //窗口大小  以此为例
    float windowWidth = 1280;
    float windowHeight = 720;
    //计算框体显示位置
    float posX = (Screen.currentResolution.width - windowWidth) / 2;
    float posY = (Screen.currentResolution.height - windowHeight) / 2;
    Rect _rect = new Rect(posX, posY, windowWidth, windowHeight);

    winTool.SetNoFrameWindow(_rect);
}

/// <summary>
/// 全屏
/// </summary>
public void FullScreen()
{
    if (Screen.fullScreen)
    {
        Screen.SetResolution(1024, 768, false);
    }
    else
    {
        Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
    }
    //等待当前帧完成 再去除边框
    StartCoroutine(IE_NoFrame());
}

private IEnumerator IE_NoFrame()
{
    yield return new WaitForEndOfFrame();
    yield return new WaitForFixedUpdate();
    if (!Screen.fullScreen)
    {
        SetNoFrame();
    }
}

//窗口拖动
public void Drag()
{
    winTool.DragWindow(currentWindow);
}

}

/* *

  • 系统方法类

  • */
    public class WindowsTools
    {

    //设置当前窗口的显示状态
    [DllImport(“user32.dll”)]
    public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);

    //获取当前激活窗口
    [DllImport(“user32.dll”, EntryPoint = “GetForegroundWindow”)]
    public static extern System.IntPtr GetForegroundWindow();

    //设置窗口边框
    [DllImport(“user32.dll”)]
    public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

    //设置窗口位置,大小
    [DllImport(“user32.dll”)]
    public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    //窗口拖动
    [DllImport(“user32.dll”)]
    public static extern bool ReleaseCapture();
    [DllImport(“user32.dll”)]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

    //边框参数
    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;//(最小化窗口)

    //最小化窗口
    public void SetMinWindows()
    {
    ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
    //具体窗口参数看这 https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
    }

    //设置无边框,并设置框体大小,位置
    public void SetNoFrameWindow(Rect rect)
    {
    SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
    bool result = SetWindowPos(GetForegroundWindow(), 0, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, SWP_SHOWWINDOW);
    }

    //拖动窗口
    public void DragWindow(IntPtr window)
    {
    ReleaseCapture();
    SendMessage(window, 0xA1, 0x02, 0);
    SendMessage(window, 0x0202, 0, 0);
    }
    }

/*xbb

  • 拖动窗口UI

  • */
    public class WindowDragObject : MonoBehaviour, IPointerDownHandler, IPointerExitHandler, IPointerUpHandler
    {
    private bool isDrag = false;

    void Update()
    {
    if (isDrag == true)
    {
    WindowsSetting.Instance.Drag();
    }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
    isDrag = false;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
    isDrag = false;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
    isDrag = true;
    }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值