Skip to content

Commit

Permalink
Cache background brush
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
dotMorten authored Sep 24, 2024
1 parent 7125984 commit d784e03
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/WinUIEx/TransparentTintBackdrop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.UI.Composition;
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Hosting;
using System;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down

0 comments on commit d784e03

Please sign in to comment.