Skip to content

Commit dbb74b0

Browse files
Address Graphics state reset issue.
1 parent 16058a3 commit dbb74b0

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

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

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

44
using System.Drawing;
5+
using System.Drawing.Drawing2D;
56
using System.Windows.Forms.VisualStyles;
67

78
namespace System.Windows.Forms;
@@ -44,39 +45,50 @@ public void RenderButton(
4445
ArgumentNullException.ThrowIfNull(paintImage);
4546
ArgumentNullException.ThrowIfNull(paintField);
4647

47-
// Clear the background over the whole button area.
48-
ClearBackground(graphics, parentBackgroundColor);
48+
GraphicsState? graphicsState = default;
4949

50-
// Use padding from ButtonDarkModeRenderer
51-
Padding padding = PaddingCore;
52-
53-
Rectangle paddedBounds = new(
54-
x: bounds.X + padding.Left,
55-
y: bounds.Y + padding.Top,
56-
width: bounds.Width - padding.Horizontal,
57-
height: bounds.Height - padding.Vertical);
58-
59-
// Draw button background and get content bounds
60-
Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault);
61-
62-
// Offset content bounds for Popup style when button is pressed
63-
// if (flatStyle == FlatStyle.Popup && state == PushButtonState.Pressed)
64-
// {
65-
// contentBounds.Offset(1, 1);
66-
// }
67-
68-
// Paint image and field using the provided delegates
69-
paintImage(contentBounds);
70-
71-
paintField(
72-
contentBounds,
73-
GetTextColor(state, isDefault),
74-
false);
75-
76-
if (focused && showFocusCues)
50+
try
51+
{
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+
56+
// Clear the background over the whole button area.
57+
ClearBackground(graphics, parentBackgroundColor);
58+
59+
// Use padding from ButtonDarkModeRenderer
60+
Padding padding = PaddingCore;
61+
62+
Rectangle paddedBounds = new(
63+
x: bounds.X + padding.Left,
64+
y: bounds.Y + padding.Top,
65+
width: bounds.Width - padding.Horizontal,
66+
height: bounds.Height - padding.Vertical);
67+
68+
// Draw button background and get content bounds
69+
Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault);
70+
71+
// Paint image and field using the provided delegates
72+
paintImage(contentBounds);
73+
74+
paintField(
75+
contentBounds,
76+
GetTextColor(state, isDefault),
77+
false);
78+
79+
if (focused && showFocusCues)
80+
{
81+
// Draw focus indicator for other styles
82+
DrawFocusIndicator(graphics, bounds, isDefault);
83+
}
84+
}
85+
finally
7786
{
78-
// Draw focus indicator for other styles
79-
DrawFocusIndicator(graphics, bounds, isDefault);
87+
if (graphicsState is not null)
88+
{
89+
// Restore the graphics state to ensure no side effects
90+
graphics.Restore(graphicsState);
91+
}
8092
}
8193
}
8294

0 commit comments

Comments
 (0)