[WinUI3] Semi Transparent Window + Click through Window

Jonathan SERRA 25 Reputation points
2023-11-06T14:51:15.5566667+00:00

Hi there,

I'm trying to make a transparent window + the ability to click through the window, in other word making the app window not capturing mouse events.

Below is my app Window which is semi transparent in which I drew a rectangle. At a certain point, I want to be able to click on the window right under my app window : in this example selecting the code you see below (which is another window, it's Visual Studio).
User's image

To make my window transparent I use the following code :

private unsafe IntPtr WndProc(HWND hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, nuint uIdSubclass, IntPtr dwRefData)
{
    if (uMsg == (uint)WindowMessage.WM_ERASEBKGND)
    {
        if (GetClientRect(hWnd, out var rect))
        {
            using var brush = Gdi32.CreateSolidBrush(new COLORREF(0, 0, 0));
            FillRect(wParam, rect, brush);
            return new IntPtr(1);
        }
    }
    else if (uMsg == (uint)WindowMessage.WM_DWMCOMPOSITIONCHANGED)
    {
        DwmApi.DwmExtendFrameIntoClientArea(hWnd, new DwmApi.MARGINS(0));
        using var rgn = Gdi32.CreateRectRgn(-2, -2, -1, -1);
        DwmApi.DwmEnableBlurBehindWindow(hWnd, new DwmApi.DWM_BLURBEHIND(true)
        {
            dwFlags = DwmApi.DWM_BLURBEHIND_Mask.DWM_BB_ENABLE | DwmApi.DWM_BLURBEHIND_Mask.DWM_BB_BLURREGION,
            hRgnBlur = rgn
        });

        return IntPtr.Zero;
    }
    else if (uMsg == 134 || uMsg == 70)
    {
        // avoid event ?
        return ComCtl32.DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }

    return ComCtl32.DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
// in MainWindow constructor, after InitializeComponent();
var presenter = FullScreenPresenter.Create();
// presenter.IsAlwaysOnTop = true;
this.AppWindow.SetPresenter(presenter);

/* Setup transparent window */
var windowHandle = new IntPtr((long)WindowNative.GetWindowHandle(this));

DwmApi.DwmExtendFrameIntoClientArea(windowHandle, new DwmApi.MARGINS(0));
using var rgn = Gdi32.CreateRectRgn(-2, -2, -1, -1);
DwmApi.DwmEnableBlurBehindWindow(windowHandle, new DwmApi.DWM_BLURBEHIND(true)
{
    dwFlags = DwmApi.DWM_BLURBEHIND_Mask.DWM_BB_ENABLE | DwmApi.DWM_BLURBEHIND_Mask.DWM_BB_BLURREGION,
    hRgnBlur = rgn
});

var brushHolder = this.As<ICompositionSupportsSystemBackdrop>();
var colorBrush = WindowsComposition.Compositor.CreateColorBrush(Windows.UI.Color.FromArgb(0, 255, 255, 255));
brushHolder.SystemBackdrop = colorBrush;

wndProcHandler = new ComCtl32.SUBCLASSPROC(WndProc);
ComCtl32.SetWindowSubclass(windowHandle, wndProcHandler, 1, IntPtr.Zero);

I tried some approach but none worked out, this one provided by Peter in particular https://github.com/castorix/WinUI3_SwapChainPanel_Layered/issues/5#issuecomment-1793536611

Thanks!

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
792 questions
{count} votes

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.