How to make Winui 3 window transparent?

Mahdi Hosseini 155 Reputation points
2023-12-18T07:08:37.57+00:00

hi, i want to make a transparent window, i used following codes:

public static void SetTransparent(Window window, bool isTransparent)
{
    var rgn = NativeMethods.CreateRectRgn(-2, -2, -1, -1);
    var dmw = new NativeValues.DWM_BLURBEHIND()
    {
        dwFlags = NativeValues.DWM_BLURBEHIND_Mask.DWM_BB_ENABLE | NativeValues.DWM_BLURBEHIND_Mask.DWM_BB_BLURREGION,
        fEnable = true,
        hRgnBlur = rgn,
    };

    NativeMethods.DwmEnableBlurBehindWindow(WindowNative.GetWindowHandle(window), ref dmw);

    ICompositionSupportsSystemBackdrop brushHolder = window.As<ICompositionSupportsSystemBackdrop>();

    if (isTransparent)
    {
        var colorBrush = WindowsCompositionHelper.Compositor.CreateColorBrush(Windows.UI.Color.FromArgb(0, 255, 255, 255));
        brushHolder.SystemBackdrop = colorBrush;
    }
    else
    {
        brushHolder.SystemBackdrop = null;
    }
}

but there is some issues:

if windows theme is Dark, window will be transparent with a border/frame + shadow around window

1

if windows theme is Light, window can not be transparent and a white background will be shown

2

so there is 3 issue:

first, transparent window should work in Light and Dark theme

second, how to remove border when window is transparent?

third, can we get fully transparent window? i mean i can access anything under window. for example if explorer is open and transparent window is opened on explorer i can access explorer from transparent window.

Windows development | Windows App SDK
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2023-12-18T08:25:02.88+00:00

    I had posted a way with a SwapChainPanel

    From some people who had tested, the border + shadow were on Windows 11 and I added DwmSetWindowAttribute at beginning to remove them

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.