[WinUI3] Semi Transparent Window + Click through Window
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).
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!