Skip to content

Commit c2e7374

Browse files
Update renderer.
1 parent 68e20cf commit c2e7374

File tree

5 files changed

+185
-81
lines changed

5 files changed

+185
-81
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/ButtonDarkModeAdapter.cs

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ internal ButtonDarkModeAdapter(ButtonBase control) : base(control) { }
1818

1919
internal override void PaintUp(PaintEventArgs e, CheckState state)
2020
{
21+
var smoothingMode = e.Graphics.SmoothingMode;
22+
e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias;
23+
2124
LayoutData layout = CommonLayout().Layout();
2225

2326
ButtonDarkModeRenderer.RenderButton(
@@ -37,10 +40,16 @@ internal override void PaintUp(PaintEventArgs e, CheckState state)
3740
textColor,
3841
drawFocus: false)
3942
);
43+
44+
e.Graphics.SmoothingMode = smoothingMode;
4045
}
4146

4247
internal override void PaintDown(PaintEventArgs e, CheckState state)
4348
{
49+
// Set the smoothing mode to AntiAlias for better rendering quality
50+
var smoothingMode = e.Graphics.SmoothingMode;
51+
e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias;
52+
4453
LayoutData layout = CommonLayout().Layout();
4554
ButtonDarkModeRenderer.RenderButton(
4655
e.Graphics,
@@ -59,10 +68,17 @@ internal override void PaintDown(PaintEventArgs e, CheckState state)
5968
textColor,
6069
drawFocus: false)
6170
);
71+
72+
// Restore the original smoothing mode
73+
e.Graphics.SmoothingMode = smoothingMode;
6274
}
6375

6476
internal override void PaintOver(PaintEventArgs e, CheckState state)
6577
{
78+
// Set the smoothing mode to AntiAlias for better rendering quality
79+
var smoothingMode = e.Graphics.SmoothingMode;
80+
e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias;
81+
6682
LayoutData layout = CommonLayout().Layout();
6783
ButtonDarkModeRenderer.RenderButton(
6884
e.Graphics,
@@ -81,6 +97,9 @@ internal override void PaintOver(PaintEventArgs e, CheckState state)
8197
textColor,
8298
drawFocus: false)
8399
);
100+
101+
// Restore the original smoothing mode
102+
e.Graphics.SmoothingMode = smoothingMode;
84103
}
85104

86105
protected override LayoutOptions Layout(PaintEventArgs e) => CommonLayout();

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/DarkModeRenderer/ButtonDarkModeRenderer.cs

+27-16
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ namespace System.Windows.Forms;
1212
/// </summary>
1313
internal static partial class ButtonDarkModeRenderer
1414
{
15-
// Magic numbers extracted as constants or static properties
16-
private const int DefaultPaddingValue = 2;
17-
1815
// Singleton instances for each renderer type
1916
internal static IButtonDarkModeRenderer StandardRenderer { get; } = new StandardButtonDarkModeRenderer();
2017
internal static IButtonDarkModeRenderer FlatRenderer { get; } = new FlatButtonDarkModeRenderer();
2118
internal static IButtonDarkModeRenderer PopupRenderer { get; } = new PopupButtonDarkModeRenderer();
2219
internal static IButtonDarkModeRenderer SystemRenderer { get; } = new SystemButtonDarkModeRenderer();
2320

2421
// Define padding values for each renderer type
25-
internal static Padding StandardPaddingCore { get; } = new(DefaultPaddingValue);
26-
internal static Padding FlatPaddingCore { get; } = new(DefaultPaddingValue);
27-
internal static Padding PopupPaddingCore { get; } = new(DefaultPaddingValue);
28-
internal static Padding SystemPaddingCore { get; } = new(DefaultPaddingValue);
22+
internal static Padding StandardPaddingCore { get; } = new(0);
23+
internal static Padding FlatPaddingCore { get; } = new(0);
24+
internal static Padding PopupPaddingCore { get; } = new(0);
25+
internal static Padding SystemPaddingCore { get; } = new(0);
2926

3027
/// <summary>
3128
/// Returns the appropriate padding for the specified flat style.
@@ -52,13 +49,8 @@ public static void DrawButtonBorder(Graphics graphics, GraphicsPath path, Color
5249
/// Clears the background with the parent's background color or the control's background color if no parent is available.
5350
/// </summary>
5451
/// <param name="graphics">Graphics context to draw on</param>
55-
/// <param name="bounds">Bounds of the area to clear</param>
56-
private static void ClearBackground(Graphics graphics, Rectangle bounds, Color parentBackgroundColor)
57-
{
58-
// Clear the background with the determined color
59-
using var brush = parentBackgroundColor.GetCachedSolidBrushScope();
60-
graphics.FillRectangle(brush, bounds);
61-
}
52+
private static void ClearBackground(Graphics graphics, Color parentBackgroundColor) =>
53+
graphics.Clear(parentBackgroundColor);
6254

6355
/// <summary>
6456
/// Renders a button with the specified properties and delegates for painting image and field.
@@ -84,24 +76,43 @@ internal static void RenderButton(
8476
};
8577

8678
// Background over the whole Button area needs to be cleared in any case.
87-
ClearBackground(graphics, bounds, parentBackgroundColor);
79+
ClearBackground(graphics, parentBackgroundColor);
8880

8981
// Use padding from ButtonDarkModeRenderer
9082
Padding padding = GetPaddingCore(FlatStyle.System);
91-
Rectangle paddedBounds = Rectangle.Inflate(bounds, -padding.Left, -padding.Top);
83+
Rectangle paddedBounds = new(
84+
x: bounds.X + padding.Left,
85+
y: bounds.Y + padding.Top,
86+
width: bounds.Width - padding.Horizontal,
87+
height: bounds.Height - padding.Vertical);
9288

9389
// Draw button background and get content bounds
9490
Rectangle contentBounds = renderer.DrawButtonBackground(graphics, paddedBounds, state, isDefault);
9591

92+
// Offset content bounds for Popup style when button is pressed
93+
if (flatStyle == FlatStyle.Popup && state == PushButtonState.Pressed)
94+
{
95+
contentBounds.Offset(1, 1);
96+
}
97+
9698
// Paint image and field using the provided delegates
9799
paintImage(contentBounds);
100+
98101
paintField(
99102
contentBounds,
100103
renderer.GetTextColor(state, isDefault),
101104
false);
102105

106+
if (focused && flatStyle == FlatStyle.System)
107+
{
108+
// Focus with the system renderer is completely surrounded by a border.
109+
renderer.DrawFocusIndicator(graphics, bounds, isDefault);
110+
return;
111+
}
112+
103113
if (focused && showFocusCues)
104114
{
115+
// Draw focus indicator for other styles
105116
renderer.DrawFocusIndicator(graphics, contentBounds, isDefault);
106117
}
107118
}

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/DarkModeRenderer/PopupButtonDarkModeRenderer.cs

-7
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ public Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushB
9090
/// </summary>
9191
public void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool isDefault)
9292
{
93-
// Save original smoothing mode
94-
SmoothingMode originalMode = graphics.SmoothingMode;
95-
graphics.SmoothingMode = SmoothingMode.AntiAlias;
96-
9793
// Create a slightly smaller rectangle for the focus indicator
9894
Rectangle focusRect = Rectangle.Inflate(contentBounds, -FocusPadding, -FocusPadding);
9995

@@ -110,9 +106,6 @@ public void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool
110106
// Draw the focus rectangle with rounded corners
111107
GraphicsPath focusPath = GetRoundedRectanglePath(focusRect, FocusCornerRadius);
112108
graphics.DrawPath(focusPen, focusPath);
113-
114-
// Restore original smoothing mode
115-
graphics.SmoothingMode = originalMode;
116109
}
117110

118111
/// <summary>

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/DarkModeRenderer/StandardButtonDarkModeRenderer.cs

-7
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushB
5353
/// </summary>
5454
public void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool isDefault)
5555
{
56-
// Save original smoothing mode
57-
SmoothingMode originalMode = graphics.SmoothingMode;
58-
graphics.SmoothingMode = SmoothingMode.AntiAlias;
59-
6056
// Create a slightly smaller rectangle for the focus indicator
6157
Rectangle focusRect = Rectangle.Inflate(contentBounds, -FocusIndicatorInset, -FocusIndicatorInset);
6258

@@ -73,9 +69,6 @@ public void DrawFocusIndicator(Graphics graphics, Rectangle contentBounds, bool
7369
// Draw the focus rectangle with rounded corners
7470
using GraphicsPath focusPath = GetRoundedRectanglePath(focusRect, FocusIndicatorCornerRadius);
7571
graphics.DrawPath(focusPen, focusPath);
76-
77-
// Restore original smoothing mode
78-
graphics.SmoothingMode = originalMode;
7972
}
8073

8174
/// <summary>

0 commit comments

Comments
 (0)