From d784e03d20ee3adf0619afd07148e8db5a052513 Mon Sep 17 00:00:00 2001 From: Morten Nielsen Date: Tue, 24 Sep 2024 16:38:21 -0700 Subject: [PATCH] Cache background brush Fixes #156 --- src/WinUIEx/TransparentTintBackdrop.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/WinUIEx/TransparentTintBackdrop.cs b/src/WinUIEx/TransparentTintBackdrop.cs index fa4c534..2c46861 100644 --- a/src/WinUIEx/TransparentTintBackdrop.cs +++ b/src/WinUIEx/TransparentTintBackdrop.cs @@ -1,4 +1,4 @@ -using Microsoft.UI.Composition; +using Microsoft.UI.Composition; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Hosting; using System; @@ -85,6 +85,9 @@ protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop backdrop?.Dispose(); brush?.Dispose(); brush = null; + if (!backgroundBrush.IsNull) + PInvoke.DeleteObject(backgroundBrush); + backgroundBrush = Windows.Win32.Graphics.Gdi.HBRUSH.Null; base.OnTargetDisconnected(disconnectedTarget); } @@ -100,12 +103,15 @@ private static void ConfigureDwm(ulong hWnd) }); } + private Windows.Win32.Graphics.Gdi.HBRUSH backgroundBrush = Windows.Win32.Graphics.Gdi.HBRUSH.Null; + private bool ClearBackground(nint hwnd, nint hdc) { if (PInvoke.GetClientRect(new Windows.Win32.Foundation.HWND(hwnd), out var rect)) { - var brush = PInvoke.CreateSolidBrush(new Windows.Win32.Foundation.COLORREF(0)); - FillRect(hdc, ref rect, brush); + if (backgroundBrush.IsNull) + backgroundBrush = PInvoke.CreateSolidBrush(new Windows.Win32.Foundation.COLORREF(0)); + FillRect(hdc, ref rect, backgroundBrush); return true; } return false;