zoukankan      html  css  js  c++  java
  • Unity 设置窗体透明

    设置窗口透明、窗口置顶、鼠标穿透

       

    方法一、

    缺点:边缘不平滑,有毛边

    参考博客:

    1、https://alastaira.wordpress.com/2015/06/15/creating-windowless-unity-applications/

    2、http://www.manew.com/thread-43230-1-1.html

    3、https://blog.csdn.net/dark00800/article/details/70314432

    关键代码

    Shader "Custom/ChromakeyTransparent" {
        Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _TransparentColourKey ("Transparent Colour Key", Color) = (0,0,0,1)
            _TransparencyTolerance ("Transparency Tolerance", Float) = 0.01 
        }
        SubShader {
            Pass {
                Tags { "RenderType" = "Opaque" }
                LOD 200
             
                CGPROGRAM
     
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
     
                struct a2v
                {
                    float4 pos : POSITION;
                    float2 uv : TEXCOORD0;
                };
     
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    float2 uv : TEXCOORD0;
                };
     
                v2f vert(a2v input)
                {
                    v2f output;
                    output.pos = mul (UNITY_MATRIX_MVP, input.pos);
                    output.uv = input.uv;
                    return output;
                }
             
                sampler2D _MainTex;
                float3 _TransparentColourKey;
                float _TransparencyTolerance;
     
                float4 frag(v2f input) : SV_Target
                {
                    // What is the colour that *would* be rendered here?
                    float4 colour = tex2D(_MainTex, input.uv);
                 
                    // Calculate the different in each component from the chosen transparency colour
                    float deltaR = abs(colour.r - _TransparentColourKey.r);
                    float deltaG = abs(colour.g - _TransparentColourKey.g);
                    float deltaB = abs(colour.b - _TransparentColourKey.b);
     
                    // If colour is within tolerance, write a transparent pixel
                    if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance)
                    {
                        return float4(0.0f, 0.0f, 0.0f, 0.0f);
                    }
     
                    // Otherwise, return the regular colour
                    return colour;
                }
                ENDCG
            }
        }
    }
    using System;
    using System.Runtime.InteropServices;
    using UnityEngine;
     
    public class TransparentWindow : MonoBehaviour
    {
        [SerializeField]
        private Material m_Material;
     
        private struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }
     
        // Define function signatures to import from Windows APIs
     
        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();
     
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
         
        [DllImport("Dwmapi.dll")]
        private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
     
     
        // Definitions of window styles
        const int GWL_STYLE = -16;
        const uint WS_POPUP = 0x80000000;
        const uint WS_VISIBLE = 0x10000000;
     
        void Start()
        {
            #if !UNITY_EDITOR
            var margins = new MARGINS() { cxLeftWidth = -1 };
     
            // Get a handle to the window
            var hwnd = GetActiveWindow();
     
            // Set properties of the window
            // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
            SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
             
            // Extend the window into the client area
            See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx 
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
            #endif
        }
     
        // Pass the output of the camera to the custom material
        // for chroma replacement
        void OnRenderImage(RenderTexture from, RenderTexture to)
        {
            Graphics.Blit(from, to, m_Material);
        }
    }

    下面是改进版,添加窗口置顶和鼠标穿透,不过依然存在毛边现象,shader还是用上面的

    using System;
    using System.Runtime.InteropServices;
    using UnityEngine;
    
     
    public class TransparentWindow :MonoBehaviour
    {
    
        [SerializeField]
        public Material m_Material;
    
        private struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }
    
        // Define function signatures to import from Windows APIs
    
        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();
    
        [DllImport("user32.dll")]
        private static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
    
        [DllImport("Dwmapi.dll")]
        private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
    
        [DllImport("user32.dll")]
        private static extern uint GetWindowLong(IntPtr hwnd,int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags);
    
        /// <summary>   
        /// 窗口置顶
        /// </summary>   
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
        /// <summary>   
        /// 得到当前活动的窗口   
        /// </summary>   
        /// <returns></returns>   
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
    
    
       
    
    const uint LWA_COLORKEY = 0x1;
        // Definitions of window styles
        const int GWL_STYLE = -16;
        const uint WS_POPUP = 0x80000000;
        const uint WS_VISIBLE = 0x10000000;
    
       
    
        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
     
        private const int GWL_EXSTYLE = (-20);
        private const int LWA_ALPHA = 0x2;
        IntPtr hwnd;
        void Start()
        {
            Application.runInBackground=true;
           var margins = new MARGINS() { cxLeftWidth = -1 };
    
            hwnd = GetActiveWindow();
    
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
    
            WindowTop();
            chuantoulong();
        }
    
        /// <summary> 
        /// 设置窗体置顶
        /// </summary> 
        private void WindowTop()
        {
            SetWindowPos(GetForegroundWindow(), -1, 0, 0, 0, 0, 1 | 2);
            
          
            uint intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
         
        }
        
        /// <summary>
        /// 鼠标穿透
        /// </summary>
        public void chuantoulong()
        {
                SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
          
        }
        public void chuantoulong_fan()
        {
            SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT);
    
        }
        void OnRenderImage(RenderTexture from, RenderTexture to)
        {
         
            Graphics.Blit(from, to, m_Material);
        }
        const int WS_BORDER = 0;
         
        
       
    }

    方法二、

    效果最佳,无毛边效果

    参考博客:

    1、https://blog.csdn.net/q493201681/article/details/65936592

    注意,需要将Camera设置为Solid Color,并将颜色改为黑色

    关键代码:

    using UnityEngine;
    using System.Collections;
    using System;
    using System.Runtime.InteropServices;
    using System.IO;
     
    /// <summary>
    /// 一共可选择三种样式
    /// </summary>
    public enum enumWinStyle
    {
        /// <summary>
        /// 置顶
        /// </summary>
        WinTop,
        /// <summary>
        /// 置顶并且透明
        /// </summary>
        WinTopApha,
        /// <summary>
        /// 置顶透明并且可以穿透
        /// </summary>
        WinTopAphaPenetrate
    }
    public class WinSetting : MonoBehaviour
    {
     
        #region Win函数常量
        private struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }
     
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
     
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
     
        [DllImport("user32.dll")]
        static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
     
        [DllImport("user32.dll")]
        static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
     
        [DllImport("Dwmapi.dll")]
        static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
        private const int WS_POPUP = 0x800000;
        private const int GWL_EXSTYLE = -20;
        private const int GWL_STYLE = -16;
        private const int WS_EX_LAYERED = 0x00080000;
        private const int WS_BORDER = 0x00800000;
        private const int WS_CAPTION = 0x00C00000;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int LWA_COLORKEY = 0x00000001;
        private const int LWA_ALPHA = 0x00000002;
        private const int WS_EX_TRANSPARENT = 0x20;
        //
        private const int ULW_COLORKEY = 0x00000001;
        private const int ULW_ALPHA = 0x00000002;
        private const int ULW_OPAQUE = 0x00000004;
        private const int ULW_EX_NORESIZE = 0x00000008;
        #endregion
        //
        public string strProduct;//项目名称
        public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
        //
        public int ResWidth;//窗口宽度
        public int ResHeight;//窗口高度
        //
        public int currentX;//窗口左上角坐标x
        public int currentY;//窗口左上角坐标y
        //
        private bool isApha;//是否透明
        private bool isAphaPenetrate;//是否要穿透窗体
        // Use this for initialization
        void Awake()
        {
            Screen.fullScreen = false;
            #if UNITY_EDITOR
            print("编辑模式不更改窗体");
            #else
            switch (WinStyle)
            {
                case enumWinStyle.WinTop:
                    isApha = false;
                    isAphaPenetrate = false;
                    break;
                case enumWinStyle.WinTopApha:
                    isApha = true;
                    isAphaPenetrate = false;
                    break;
                case enumWinStyle.WinTopAphaPenetrate:
                    isApha = true;
                    isAphaPenetrate = true;
                    break;
            }
            //
            IntPtr hwnd = FindWindow(null, strProduct);
            //
            if (isApha)
            {
                //去边框并且透明
                SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
                int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
                if (isAphaPenetrate)//是否透明穿透窗体
                {
                    SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
                }
                //
                SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
                SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
                var margins = new MARGINS() { cxLeftWidth = -1 };
                //
                DwmExtendFrameIntoClientArea(hwnd, ref margins);
            }
            else
            {
                //单纯去边框
                SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
                SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
            }
            #endif
        }
    }

    下面是改良版,将功能拆分,便于扩展

    using UnityEngine;
    using System.Collections;
    using System;
    using System.Runtime.InteropServices;
    using System.IO;
    
    /// <summary>
    /// 一共可选择三种样式
    /// </summary>
    public enum enumWinStyle
    {
        /// <summary>
        /// 置顶
        /// </summary>
        WinTop,
        /// <summary>
        /// 透明
        /// </summary>
        Apha,
        /// <summary>
        /// 置顶并且透明
        /// </summary>
        WinTopApha,
        /// <summary>
        /// 置顶透明并且可以穿透
        /// </summary>
        WinTopAphaPenetrate
    }
    public class WinSetting : MonoBehaviour
    {
        
    
        #region Win函数常量
        private struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }
        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();
    
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
        [DllImport("user32.dll")]
        static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    
        [DllImport("user32.dll")]
        static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
    
        [DllImport("Dwmapi.dll")]
        static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
        private const int WS_POPUP = 0x800000;
        private const int GWL_EXSTYLE = -20;
        private const int GWL_STYLE = -16;
        private const int WS_EX_LAYERED = 0x00080000;
        private const int WS_BORDER = 0x00800000;
        private const int WS_CAPTION = 0x00C00000;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int LWA_COLORKEY = 0x00000001;
        private const int LWA_ALPHA = 0x00000002;
        private const int WS_EX_TRANSPARENT = 0x20;
        //
        private const int ULW_COLORKEY = 0x00000001;
        private const int ULW_ALPHA = 0x00000002;
        private const int ULW_OPAQUE = 0x00000004;
        private const int ULW_EX_NORESIZE = 0x00000008;
        #endregion
        //
        public string strProduct;//项目名称
        public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
        //
        public int ResWidth;//窗口宽度
        public int ResHeight;//窗口高度
        //
        public int currentX;//窗口左上角坐标x
        public int currentY;//窗口左上角坐标y
        //
        private bool isWinTop;//是否置顶
        private bool isApha;//是否透明
        private bool isAphaPenetrate;//是否要穿透窗体
    
        IntPtr hwnd;
    
        // Use this for initialization
        void Awake()
        {
            Screen.fullScreen = false;
            //hwnd = FindWindow(null, strProduct);//可作用于其他进程的窗口
            hwnd = GetActiveWindow();
    
            //测试效果
            RemoveRim();
        }
        //去掉边框且去掉任务栏图标
        void RemoveRimAndIcon()
        {
            SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
        }
        //去掉边框
        void RemoveRim()
        {
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
        }
        //置顶
        void SetTop()
        {
            SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
        }
        //设置透明:注意
        //1、一定要先调用去掉边框的函数
        //2、将camera 设置为Solid color,并将颜色设置为黑色
        void SetTransparency()
        {
            var margins = new MARGINS() { cxLeftWidth = -1 };
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
        }
        //鼠标穿透窗体
        void SetMouseThrough()
        {
            int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
        }
      
    }

     去掉加载时窗体闪一下的效果

    using UnityEngine;
    using System.Collections;
    using System;
    using System.Runtime.InteropServices;
    using System.IO;
    
    /// <summary>
    /// 一共可选择三种样式
    /// </summary>
    public enum enumWinStyle
    {
        /// <summary>
        /// 置顶
        /// </summary>
        WinTop,
        /// <summary>
        /// 透明
        /// </summary>
        Apha,
        /// <summary>
        /// 置顶并且透明
        /// </summary>
        WinTopApha,
        /// <summary>
        /// 置顶透明并且可以穿透
        /// </summary>
        WinTopAphaPenetrate
    }
    public class WinSetting : MonoBehaviour
    {
    
        #region Win函数常量
        private struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        }
        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();
    
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
        [DllImport("user32.dll")]
        static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    
        [DllImport("user32.dll")]
        static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
    
        [DllImport("Dwmapi.dll")]
        static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
        private const int WS_POPUP = 0x800000;
        private const int GWL_EXSTYLE = -20;
        private const int GWL_STYLE = -16;
        private const int WS_EX_LAYERED = 0x00080000;
        private const int WS_BORDER = 0x00800000;
        private const int WS_CAPTION = 0x00C00000;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int LWA_COLORKEY = 0x00000001;
        private const int LWA_ALPHA = 0x00000002;
        private const int WS_EX_TRANSPARENT = 0x20;
        //
        private const int ULW_COLORKEY = 0x00000001;
        private const int ULW_ALPHA = 0x00000002;
        private const int ULW_OPAQUE = 0x00000004;
        private const int ULW_EX_NORESIZE = 0x00000008;
        #endregion
        //
        public string strProduct;//项目名称
        public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
        //
        public int ResWidth;//窗口宽度
        public int ResHeight;//窗口高度
        //
        public int currentX;//窗口左上角坐标x
        public int currentY;//窗口左上角坐标y
        //
        private bool isWinTop;//是否置顶
        private bool isApha;//是否透明
        private bool isAphaPenetrate;//是否要穿透窗体
    
        IntPtr hwnd;
    
        // Use this for initialization
        void Awake()
        {
            
            Screen.fullScreen = false;
    //#if UNITY_EDITOR
     //       print("编辑模式不更改窗体");
    //#else
            switch (WinStyle)
            {
                case enumWinStyle.WinTop:
                    isApha = false;
                    isAphaPenetrate = false;
                    break;
                case enumWinStyle.Apha:
                    isWinTop = false;
                    isApha = true;
                    isAphaPenetrate = false;
                    break;
                case enumWinStyle.WinTopApha:
                    isApha = true;
                    isAphaPenetrate = false;
                    break;
                case enumWinStyle.WinTopAphaPenetrate:
                    isApha = true;
                    isAphaPenetrate = true;
                    break;
            }
            
            //
            //IntPtr hwnd = FindWindow(null, strProduct);
             hwnd = GetActiveWindow();
    
            //
            if (isApha)
            {
                //去边框并且透明
                SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
                int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
                if (isAphaPenetrate)//是否透明穿透窗体
                {
                    SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
                }
                //
                SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
    
                //保持中间位置:因为是从左上角算起的,所以获得屏幕像素后要减去窗体宽高的一半
                currentX = Screen.currentResolution.width / 2-320/2;
                currentY = Screen.currentResolution.height / 2-270/2;
    
                SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
                var margins = new MARGINS() { cxLeftWidth = -1 };
                //
                DwmExtendFrameIntoClientArea(hwnd, ref margins);
               
            }
            else
            {
                //单纯去边框
                SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
                SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
            }
            Debug.Log(WinStyle);
    //#endif
        }
    
        void OnApplicationQuit()
        {
            //程序退出的时候设置窗体为0像素,从打开到走到awake也需要一定是时间
            //会先有窗体边框,然后透明,这样会有闪一下的效果,
            //设置窗体为0像素后,下次打开是就是0像素,走到awake再设置回来正常的窗口大小
            //便能解决程序加载时会闪白色边框的现象
            SetWindowPos(hwnd, -1, currentX, currentY, 0, 0, SWP_SHOWWINDOW);
        }
    }

     方法三、

    参考博客:

    1、http://blog.sina.com.cn/s/blog_c0e6ab9b0102wn2s.html

    注意,需要将Camera设置为Solid Color,并将颜色改为黑色,有毛边

    关键代码:

    //using System;
    //using System.Runtime.InteropServices;
    //using UnityEngine;
    //public class WindowMod : MonoBehaviour
    //{
    //    public enum appStyle
    //    {
    //        FullScreen,
    //        WindowedFullScreen,
    //        Windowed,
    //        WindowedWithoutBorder
    //    }
    //    public enum zDepth
    //    {
    //        Normal,
    //        Top,
    //        TopMost
    //    }
    //    private const uint SWP_SHOWWINDOW = 64u;
    //    private const int GWL_STYLE = -16;
    //    private const int WS_BORDER = 1;
    //    private const int GWL_EXSTYLE = -20;
    //    private const int WS_CAPTION = 12582912;
    //    private const int WS_POPUP = 8388608;
    //    private const int SM_CXSCREEN = 0;
    //    private const int SM_CYSCREEN = 1;
    //    public WindowMod.appStyle AppWindowStyle = WindowMod.appStyle.WindowedFullScreen;
    //    public WindowMod.zDepth ScreenDepth;
    //    public int windowLeft = 10;
    //    public int windowTop = 10;
    //    public int windowWidth = 800;
    //    public int windowHeight = 600;
    //    private Rect screenPosition;
    //    private IntPtr HWND_TOP = new IntPtr(0);
    //    private IntPtr HWND_TOPMOST = new IntPtr(-1);
    //    private IntPtr HWND_NORMAL = new IntPtr(-2);
    //    private int Xscreen;
    //    private int Yscreen;
    //    private int i;
    //    [DllImport("user32.dll")]
    //    private static extern IntPtr GetForegroundWindow();
    //    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    //    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", CharSet = CharSet.Auto)]
    //    public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    //    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    //    public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP);
    //    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    //    public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent);
    //    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    //    public static extern IntPtr GetParent(IntPtr hChild);
    //    [DllImport("User32.dll")]
    //    public static extern IntPtr GetSystemMetrics(int nIndex);
    //    private void Start()
    //    {
    //        this.Xscreen = (int)WindowMod.GetSystemMetrics(0);
    //        this.Yscreen = (int)WindowMod.GetSystemMetrics(1);
    //        if (this.AppWindowStyle == WindowMod.appStyle.FullScreen)
    //        {
    //            Screen.SetResolution(this.Xscreen, this.Yscreen, true);
    //        }
    //        if (this.AppWindowStyle == WindowMod.appStyle.WindowedFullScreen)
    //        {
    //            Screen.SetResolution(this.Xscreen - 1, this.Yscreen - 1, false);
    //            this.screenPosition = new Rect(0f, 0f, (float)(this.Xscreen - 1), (float)(this.Yscreen - 1));
    //        }
    //        if (this.AppWindowStyle == WindowMod.appStyle.Windowed)
    //        {
    //            Screen.SetResolution(this.windowWidth, this.windowWidth, false);
    //        }
    //        if (this.AppWindowStyle == WindowMod.appStyle.WindowedWithoutBorder)
    //        {
    //            Screen.SetResolution(this.windowWidth, this.windowWidth, false);
    //            this.screenPosition = new Rect((float)this.windowLeft, (float)this.windowTop, (float)this.windowWidth, (float)this.windowWidth);
    //        }
    //    }
    //    private void Update()
    //    {
    //        if (this.i < 5)
    //        {
    //            if (this.AppWindowStyle == WindowMod.appStyle.WindowedFullScreen)
    //            {
    //                WindowMod.SetWindowLong(WindowMod.GetForegroundWindow(), -16, 369164288);
    //                if (this.ScreenDepth == WindowMod.zDepth.Normal)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
    //                }
    //                if (this.ScreenDepth == WindowMod.zDepth.Top)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
    //                }
    //                if (this.ScreenDepth == WindowMod.zDepth.TopMost)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
    //                }
    //                WindowMod.ShowWindow(WindowMod.GetForegroundWindow(), 3);
    //            }
    //            if (this.AppWindowStyle == WindowMod.appStyle.Windowed)
    //            {
    //                if (this.ScreenDepth == WindowMod.zDepth.Normal)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, 0, 0, 0, 0, 3u);
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, 0, 0, 0, 0, 35u);
    //                }
    //                if (this.ScreenDepth == WindowMod.zDepth.Top)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, 0, 0, 0, 0, 3u);
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, 0, 0, 0, 0, 35u);
    //                }
    //                if (this.ScreenDepth == WindowMod.zDepth.TopMost)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, 0, 0, 0, 0, 3u);
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, 0, 0, 0, 0, 35u);
    //                }
    //            }
    //            if (this.AppWindowStyle == WindowMod.appStyle.WindowedWithoutBorder)
    //            {
    //                WindowMod.SetWindowLong(WindowMod.GetForegroundWindow(), -16, 369164288);
    //                if (this.ScreenDepth == WindowMod.zDepth.Normal)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
    //                }
    //                if (this.ScreenDepth == WindowMod.zDepth.Top)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
    //                }
    //                if (this.ScreenDepth == WindowMod.zDepth.TopMost)
    //                {
    //                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
    //                }
    //            }
    //        }
    //        this.i++;
    //    }
    //}
    
    using UnityEngine;
    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    
    public class WindowMod : MonoBehaviour
    {
    
        public Rect screenPosition;
    
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hwnd, int _nIndex);
    
        [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, int uFlags);
    
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
    
        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, int dwFlags);
    
        const int SWP_SHOWWINDOW = 0x0040;
        const int GWL_EXSTYLE = -20;
        const int GWL_STYLE = -16;
        const int WS_CAPTION = 0x00C00000;
        const int WS_BORDER = 0x00800000;
        const int WS_EX_LAYERED = 0x80000;
        public const int LWA_ALPHA = 0x2;
        public const int LWA_COLORKEY = 0x1;
    
        private IntPtr handle;
    
        void Start()
        {
            handle = GetForegroundWindow();
            SetWindowLong(handle, GWL_EXSTYLE, WS_EX_LAYERED);
            SetWindowLong(handle, GWL_STYLE, GetWindowLong(handle, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
            SetWindowPos(handle, -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
    
            //把黑色透明化,不工作
         //   SetLayeredWindowAttributes(handle, 0, 100, LWA_COLORKEY);
    
            //把整个窗口透明化,工作
            SetLayeredWindowAttributes(handle, 0, 100, LWA_ALPHA);
        }
    
        void LateUpdate()
        {
    
        }
    }
    

    注:去掉边框的巧妙方法,适用于任何unity打包出来的exe,而且不用写一句代码。

    步骤:

    1、输入cmd,打开命令行。

    2、在命令行上输入exe的路径  ******.exe

    3、然后在后面敲一个空格,输入-popupwindow

    4、回车

    这样操作下来,程序运行起来是没有边框的。

    同样:将-popupwindow 改为,-nolog 程序运行起来就会没有output_log.txt log文件。

    妙用还有很多,请参考官方文档https://docs.unity3d.com/Manual/CommandLineArguments.html

  • 相关阅读:
    ubuntu远程windows桌面
    spring boot 给返回值加状态 BaseData
    spring boot 拦截异常 统一处理
    IntelliJ IDEA spring boot 远程Ddbug调试
    IntelliJ IDEA 常用插件
    spring boot 请求地址带有.json 兼容处理
    spring boot 接口返回值去掉为null的字段
    spring boot 集成disconf
    Spring boot 自定义拦截器
    Linux下安装MySQL
  • 原文地址:https://www.cnblogs.com/Jason-c/p/9414573.html
Copyright © 2011-2022 走看看