Skip to content

Commit c9252f6

Browse files
Address Graphics state reset issue.
1 parent 118a672 commit c9252f6

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

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

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,37 @@ public void RenderButton(
4444
ArgumentNullException.ThrowIfNull(paintImage);
4545
ArgumentNullException.ThrowIfNull(paintField);
4646

47-
// Clear the background over the whole button area.
48-
ClearBackground(graphics, parentBackgroundColor);
49-
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)
47+
// Scope the graphics state so all changes are reverted after rendering
48+
using (new GraphicsStateScope(graphics))
7749
{
78-
// Draw focus indicator for other styles
79-
DrawFocusIndicator(graphics, bounds, isDefault);
50+
// Clear the background over the whole button area.
51+
ClearBackground(graphics, parentBackgroundColor);
52+
53+
// Use padding from ButtonDarkModeRenderer
54+
Padding padding = PaddingCore;
55+
56+
Rectangle paddedBounds = new(
57+
x: bounds.X + padding.Left,
58+
y: bounds.Y + padding.Top,
59+
width: bounds.Width - padding.Horizontal,
60+
height: bounds.Height - padding.Vertical);
61+
62+
// Draw button background and get content bounds
63+
Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault);
64+
65+
// Paint image and field using the provided delegates
66+
paintImage(contentBounds);
67+
68+
paintField(
69+
contentBounds,
70+
GetTextColor(state, isDefault),
71+
false);
72+
73+
if (focused && showFocusCues)
74+
{
75+
// Draw focus indicator for other styles
76+
DrawFocusIndicator(graphics, bounds, isDefault);
77+
}
8078
}
8179
}
8280

0 commit comments

Comments
 (0)