Skip to content

Commit defba22

Browse files
committed
Draw BackgroundImage for button under DarMode
1 parent 8308b5c commit defba22

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ private protected override bool OwnerDraw
166166
// The SystemRenderer cannot render images. So, we flip to our
167167
// own DarkMode renderer, if we need to render images, except if...
168168
&& Image is null
169-
169+
// ...or a BackgroundImage, except if...
170+
&& BackgroundImage is null
170171
// ...the user wants to opt out of implicit DarkMode rendering.
171172
&& DarkModeRequestState is true
172173

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,31 @@ internal void PaintField(
545545
/// <summary>
546546
/// Draws the button's image.
547547
/// </summary>
548+
#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.
548549
internal void PaintImage(PaintEventArgs e, LayoutData layout)
549550
{
551+
if (Application.IsDarkModeEnabled && Control.DarkModeRequestState is true && Control.BackgroundImage is not null)
552+
{
553+
Rectangle bounds = Control.ClientRectangle;
554+
bounds.Inflate(-ButtonBorderSize, -ButtonBorderSize);
555+
ControlPaint.DrawBackgroundImage(
556+
e.GraphicsInternal,
557+
Control.BackgroundImage,
558+
Color.Transparent,
559+
Control.BackgroundImageLayout,
560+
Control.ClientRectangle,
561+
bounds,
562+
Control.DisplayRectangle.Location,
563+
Control.RightToLeft);
564+
}
565+
550566
if (Control.Image is not null)
551567
{
552568
// Setup new clip region & draw
553569
DrawImageCore(e.GraphicsInternal, Control.Image, layout.ImageBounds, layout.ImageStart, layout);
554570
}
555571
}
572+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
556573

557574
internal static LayoutOptions CommonLayout(
558575
Rectangle clientRectangle,

0 commit comments

Comments
 (0)