Skip to content

Commit 3d5c2a1

Browse files
Limit the DarkModeAdapter to Button-derived classes.
1 parent c505a15 commit 3d5c2a1

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,15 +960,15 @@ internal override Size GetPreferredSizeCore(Size proposedConstraints)
960960
return LayoutUtils.UnionSizes(preferredSize + Padding.Size, MinimumSize);
961961
}
962962

963+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
963964
internal ButtonBaseAdapter Adapter
964965
{
965966
get
966967
{
967-
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
968968
if (_adapter is null
969969
|| FlatStyle != _cachedAdapterType)
970970
{
971-
if (Application.IsDarkModeEnabled)
971+
if (Application.IsDarkModeEnabled && this is Button)
972972
{
973973
_adapter = CreateDarkModeAdapter();
974974
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ internal abstract partial class ButtonBaseAdapter
2323
/// <summary>
2424
/// Returns a darkened color according to the required color contrast ratio.
2525
/// </summary>
26-
private protected static Color GetContrastingBorderColor(Color buttonBorderShadowColor) => Color.FromArgb(
27-
buttonBorderShadowColor.A,
28-
(int)(buttonBorderShadowColor.R * 0.8f),
29-
(int)(buttonBorderShadowColor.G * 0.8f),
30-
(int)(buttonBorderShadowColor.B * 0.8f));
26+
private protected static Color GetContrastingBorderColor(Color buttonBorderShadowColor) =>
27+
Color.FromArgb(
28+
buttonBorderShadowColor.A,
29+
(int)(buttonBorderShadowColor.R * 0.8f),
30+
(int)(buttonBorderShadowColor.G * 0.8f),
31+
(int)(buttonBorderShadowColor.B * 0.8f));
3132

3233
internal void Paint(PaintEventArgs e)
3334
{
@@ -48,6 +49,7 @@ internal void Paint(PaintEventArgs e)
4849
internal virtual Size GetPreferredSizeCore(Size proposedSize)
4950
{
5051
LayoutOptions? options = default;
52+
5153
using (var screen = GdiCache.GetScreenHdc())
5254
using (PaintEventArgs e = new(screen, default))
5355
{
@@ -277,11 +279,10 @@ private void Draw3DBorderRaised(IDeviceContext deviceContext, ref Rectangle boun
277279
hdc.DrawLine(topLeftInsetPen, p2, p3); // Left (up-down)
278280

279281
// Bottom + right inset
280-
281282
using CreatePenScope bottomRightInsetPen = new(
282283
disabledHighContrast
283-
? colors.WindowDisabled
284-
: stockColor ? SystemColors.ControlDark : colors.ButtonShadow);
284+
? colors.WindowDisabled
285+
: stockColor ? SystemColors.ControlDark : colors.ButtonShadow);
285286

286287
p1.Offset(0, -1); // Need to paint last pixel too.
287288
hdc.DrawLine(bottomRightInsetPen, p3, p4); // Bottom (left-right)
@@ -342,6 +343,7 @@ internal static void DrawFlatBorderWithSize(
342343
g.FillRectangle(brush, right);
343344
g.FillRectangle(brush, top);
344345
g.FillRectangle(brush, bottom);
346+
345347
return;
346348
}
347349

0 commit comments

Comments
 (0)