unity PC打包去除边界 实现Win7输入法切换

本文介绍了一个Unity脚本,用于在不同窗口模式间切换,包括全屏、无边框全屏和窗口模式。通过调用Windows API函数,可以改变窗口大小、位置和层级,实现灵活的窗口管理。
  • using System;
    //using System.Management;
    using System.Collections;
    
    using System.Runtime.InteropServices;
    
    using System.Diagnostics;
    
    using UnityEngine;
    
    public class WindowMod : MonoBehaviour
    
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags);
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hWnd, int dwNewLong);
        [DllImport("user32.dll")]
        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
        [DllImport("user32.dll")]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP);
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent);
        [DllImport("user32.dll")]
        public static extern IntPtr GetParent(IntPtr hChild);
        [DllImport("user32.dll")]
        public static extern IntPtr GetSystemMetrics(int nIndex);
    
        public enum appStyle
        {
            FullScreen = 0,
            WindowedFullScreen = 1,
            Windowed = 2,
            WindowedWithoutBorder = 3,
            WindowedFullScreenWithoutBorder = 4,
        }
        public appStyle AppWindowStyle = appStyle.WindowedFullScreen;
    
        public enum zDepth
        {
            Normal = 0,
            Top = 1,
            TopMost = 2,
        }
        public zDepth ScreenDepth = zDepth.Normal;
    
        public int windowLeft = 10;
        public int windowTop = 10;
    
        public int windowWidth = 800;
        public int windowHeight = 600;
    
        const uint SWP_SHOWWINDOW = 0x0040;
        const int GWL_STYLE = -16;
        const int WS_BORDER = 1;
        private Rect screenPosition;
        private const int GWL_EXSTYLE = (-20);
        private const int WS_CAPTION = 0xC00000;
        private const int WS_POPUP = 0x800000;
        IntPtr HWND_TOP = new IntPtr(0);
        IntPtr HWND_TOPMOST = new IntPtr(-1);
        IntPtr HWND_NORMAL = new IntPtr(-2);
    
        private const int SM_CXSCREEN = 0x00000000;
        private const int SM_CYSCREEN = 0x00000001;
    
        int Xscreen;
        int Yscreen;
        void Start()
        {
    
            Xscreen = (int)GetSystemMetrics(SM_CXSCREEN);
            Yscreen = (int)GetSystemMetrics(SM_CYSCREEN);
    
            if ((int)AppWindowStyle == 0)
            {
                Screen.SetResolution(Xscreen, Yscreen, true);
            }
            else if ((int)AppWindowStyle == 1)
            {
                Screen.SetResolution(Xscreen - 1, Yscreen - 1, false);
                screenPosition = new Rect(0, 0, Xscreen - 1, Yscreen - 1);
            }
            else if ((int)AppWindowStyle == 2)
            {
                Screen.SetResolution(windowWidth, windowWidth, false);
            }
            else if ((int)AppWindowStyle == 3)
            {
                Screen.SetResolution(windowWidth, windowWidth, false);
                screenPosition = new Rect(windowLeft, windowTop, windowWidth, windowWidth);
            }
            else if ((int)AppWindowStyle == 4)
            {
                Screen.SetResolution(Screen.width, Screen.height, false);
                //Screen.SetResolution( Screen.height*4/3 , Screen.height , false);
                screenPosition = new Rect(0, 0, Screen.width, Screen.height);
            }
    
        }
    
        int i = 0;
        void Update()
        {
    
            if (i < 5)
            {
                if ((int)AppWindowStyle == 1)
                {
                    SetWindowLong(GetForegroundWindow(), -16, 369164288);
                    if ((int)ScreenDepth == 0)
                        SetWindowPos(GetForegroundWindow(), HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    if ((int)ScreenDepth == 1)
                        SetWindowPos(GetForegroundWindow(), HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    if ((int)ScreenDepth == 2)
                        SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    ShowWindow(GetForegroundWindow(), 3);
                }
    
                if ((int)AppWindowStyle == 2)
                {
                    if ((int)ScreenDepth == 0)
                    {
                        SetWindowPos(GetForegroundWindow(), HWND_NORMAL, 0, 0, 0, 0, 0x0001 | 0x0002);
                        SetWindowPos(GetForegroundWindow(), HWND_NORMAL, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020);
                    }
                    if ((int)ScreenDepth == 1)
                    {
                        SetWindowPos(GetForegroundWindow(), HWND_TOP, 0, 0, 0, 0, 0x0001 | 0x0002);
                        SetWindowPos(GetForegroundWindow(), HWND_TOP, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020);
                    }
                    if ((int)ScreenDepth == 2)
                    {
                        SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002);
                        SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020);
                    }
    
                }
    
                if ((int)AppWindowStyle == 3)
                {
                    SetWindowLong(GetForegroundWindow(), -16, 369164288);
                    if ((int)ScreenDepth == 0)
                        SetWindowPos(GetForegroundWindow(), HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    if ((int)ScreenDepth == 1)
                        SetWindowPos(GetForegroundWindow(), HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    if ((int)ScreenDepth == 2)
                        SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                }
                if ((int)AppWindowStyle == 4)
                {
                    SetWindowLong(GetForegroundWindow(), -16, 369164288);
                    if ((int)ScreenDepth == 0)
                        SetWindowPos(GetForegroundWindow(), HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    if ((int)ScreenDepth == 1)
                        SetWindowPos(GetForegroundWindow(), HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                    if ((int)ScreenDepth == 2)
                        SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                }
            }
            i++;
    
        }
    
    }

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值