Windows: How to achieve a better-performing transparent window?

I referred to the link below to implement a transparent window in Unity, using SetLayeredWindowAttributes() to cull the black parts with a value of 0. [SOLVED!] Windows: Transparent window with opaque contents (LWA_COLORKEY)? - Unity Engine - Unity Discussions

I’ve packaged my Unity game and it runs correctly with a transparent window, but the frame rate of Windows (desktop) becomes very low. I can clearly feel the lag when dragging the file manager. I can assure that Unity is not running complex logic, and the frame rate I get from Time.deltaTime is also normal, so it doesn’t seem to be a problem with the game itself.

I guess that Windows is taking too much time to cull opaque pixels, which slows down the rendering of the entire desktop? Is there a good solution to this, so that it doesn’t affect the desktop rendering, or is there another way to render transparent windows?

Thanks.

Here is my codes:

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

public class WindowBackground
{
    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 windowHandle, int nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr windowHandle, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowPos(IntPtr windowHandle, int windowHandleInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    [DllImport("Dwmapi.dll")]
    static extern uint DwmExtendFrameIntoClientArea(IntPtr windowHandle, ref MARGINS margins);
    [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
    private static extern uint SetLayeredWindowAttributes(IntPtr windowHandle, int crKey, int bAlpha, int dwFlags);

    private const int GWL_STYLE = -16;
    private const int GWL_EXSTYLE = -20;
    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;

    public static void Init(IntPtr windowHandle)
    {
        Application.targetFrameRate = 60;
        var productName = Application.productName;
#if ! UNITY_EDITOR
        int intExTemp = GetWindowLong(windowHandle, GWL_EXSTYLE);
        SetWindowLong(windowHandle, GWL_EXSTYLE, intExTemp | WS_EX_LAYERED | WS_EX_TRANSPARENT);
        SetWindowLong(windowHandle, GWL_STYLE, GetWindowLong(windowHandle, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
        SetWindowPos(windowHandle, -1, 0, 0, 1920, 1080, SWP_SHOWWINDOW); 
        var margins = new MARGINS() { cxLeftWidth = -1 };
        DwmExtendFrameIntoClientArea(windowHandle, ref margins);
        SetLayeredWindowAttributes(windowHandle, 0, 255, LWA_COLORKEY);
#endif
    }
}

Have you found a fix to this issue? I’m also having frame problems in my build when using a transparent window. Even with an almost empty scene.

Hey! You should check out the second half of this video https://www.youtube.com/watch?v=RqgsGaMPZTw&t=72s — don’t use SetLayeredWindowAttributes .