Skip to content

Commit 3c5b282

Browse files
Draw background image for button in dark mode (#13809)
<!-- Please read CONTRIBUTING.md before submitting a pull request --> Fixes #13720 ## Proposed changes - Add `DrawBackgroundImage` in the method `PaintImage` of the `ButtonBaseAdapter.cs` <!-- We are in TELL-MODE the following section must be completed --> ## Customer Impact - Button background image can be displayed in Winform dark mode ## Regression? - Yes ## Risk - Minimal <!-- end TELL-MODE --> ## Screenshots <!-- Remove this section if PR does not change UI --> ### Before The button background images are no longer displayed in dark mode. <img width="448" height="487" alt="Image" src="https://github.com/user-attachments/assets/be3e6fac-7280-4fa9-a600-4ed21d8f4901" /> ### After The button background images can be displayed in dark mode <img width="797" height="707" alt="image" src="https://github.com/user-attachments/assets/96431537-ef63-484e-97c6-7bbb904231ca" /> ## Test methodology <!-- How did you ensure quality? --> - Manually ## Test environment(s) <!-- Remove any that don't apply --> - .net 10.0.0-rc.1.25413.101 <!-- Mention language, UI scaling, or anything else that might be relevant --> ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/13809)
2 parents 8308b5c + defba22 commit 3c5b282

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)