Skip to content

Commit a481476

Browse files
Update Reset GraphicState approach per Review.
1 parent 95f6865 commit a481476

File tree

1 file changed

+2
-16
lines changed

1 file changed

+2
-16
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/DarkMode/ButtonDarkModeRendererBase.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Drawing;
5-
using System.Drawing.Drawing2D;
65
using System.Windows.Forms.VisualStyles;
76

87
namespace System.Windows.Forms;
@@ -45,14 +44,9 @@ public void RenderButton(
4544
ArgumentNullException.ThrowIfNull(paintImage);
4645
ArgumentNullException.ThrowIfNull(paintField);
4746

48-
GraphicsState? graphicsState = default;
49-
50-
try
47+
// Scope the graphics state so all changes are reverted after rendering
48+
using (new GraphicsStateScope(graphics))
5149
{
52-
// Save the graphics state, so we can restore it later and the
53-
// overrides do not to be concerned with restoring the state.
54-
graphicsState = graphics.Save();
55-
5650
// Clear the background over the whole button area.
5751
ClearBackground(graphics, parentBackgroundColor);
5852

@@ -82,14 +76,6 @@ public void RenderButton(
8276
DrawFocusIndicator(graphics, bounds, isDefault);
8377
}
8478
}
85-
finally
86-
{
87-
if (graphicsState is not null)
88-
{
89-
// Restore the graphics state to ensure no side effects
90-
graphics.Restore(graphicsState);
91-
}
92-
}
9379
}
9480

9581
public abstract Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault);

0 commit comments

Comments
 (0)