Skip to content

Commit d784e03

Browse files
authored
Cache background brush
Fixes #156
1 parent 7125984 commit d784e03

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/WinUIEx/TransparentTintBackdrop.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.UI.Composition;
1+
using Microsoft.UI.Composition;
22
using Microsoft.UI.Xaml;
33
using Microsoft.UI.Xaml.Hosting;
44
using System;
@@ -85,6 +85,9 @@ protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop
8585
backdrop?.Dispose();
8686
brush?.Dispose();
8787
brush = null;
88+
if (!backgroundBrush.IsNull)
89+
PInvoke.DeleteObject(backgroundBrush);
90+
backgroundBrush = Windows.Win32.Graphics.Gdi.HBRUSH.Null;
8891
base.OnTargetDisconnected(disconnectedTarget);
8992
}
9093

@@ -100,12 +103,15 @@ private static void ConfigureDwm(ulong hWnd)
100103
});
101104
}
102105

106+
private Windows.Win32.Graphics.Gdi.HBRUSH backgroundBrush = Windows.Win32.Graphics.Gdi.HBRUSH.Null;
107+
103108
private bool ClearBackground(nint hwnd, nint hdc)
104109
{
105110
if (PInvoke.GetClientRect(new Windows.Win32.Foundation.HWND(hwnd), out var rect))
106111
{
107-
var brush = PInvoke.CreateSolidBrush(new Windows.Win32.Foundation.COLORREF(0));
108-
FillRect(hdc, ref rect, brush);
112+
if (backgroundBrush.IsNull)
113+
backgroundBrush = PInvoke.CreateSolidBrush(new Windows.Win32.Foundation.COLORREF(0));
114+
FillRect(hdc, ref rect, backgroundBrush);
109115
return true;
110116
}
111117
return false;

0 commit comments

Comments
 (0)