Unity3D Windows平台窗口置顶全屏

一、效果

在打包后的程序启动过程,无任点击过什么界面,程序启动完后都能显示在窗口最前面,并且全屏无边框显示。

二、思路

主要是对Window编程,引入user32.dll调用window的API对窗口进行操作

三、实现

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
public class WindowAPI : MonoBehaviour
{
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint wFlags);

    public Rect screenPosition;
    const uint SWP_SHOWWINDOW = 0x0040;
    const int HWND_TOP = 0;
    const int HWND_TOPMOST = -1;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;

    void Start()
    { 
        IntPtr ptr = FindWindow(null, "TestAnimation"); //TestAnimation为程序名,需要替换
        SetWindowLong(ptr, GWL_STYLE, WS_BORDER);   //窗口全屏
        SetWindowPos(ptr, (IntPtr)HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);   //窗口置顶
    }
}

 

你可能感兴趣的:(Unity3D)