Skip to content
1 change: 1 addition & 0 deletions src/System.Windows.Forms/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
override System.Windows.Forms.Button.BackgroundImage.set -> void
static System.Windows.Forms.Application.SetColorMode(System.Windows.Forms.SystemColorMode systemColorMode) -> void
static System.Windows.Forms.TaskDialog.ShowDialogAsync(nint hwndOwner, System.Windows.Forms.TaskDialogPage! page, System.Windows.Forms.TaskDialogStartupLocation startupLocation = System.Windows.Forms.TaskDialogStartupLocation.CenterOwner) -> System.Threading.Tasks.Task<System.Windows.Forms.TaskDialogButton!>!
static System.Windows.Forms.TaskDialog.ShowDialogAsync(System.Windows.Forms.IWin32Window! owner, System.Windows.Forms.TaskDialogPage! page, System.Windows.Forms.TaskDialogStartupLocation startupLocation = System.Windows.Forms.TaskDialogStartupLocation.CenterOwner) -> System.Threading.Tasks.Task<System.Windows.Forms.TaskDialogButton!>!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ public virtual DialogResult DialogResult
}
}

public override Image? BackgroundImage
{
set
{
base.BackgroundImage = value;

if (Application.IsDarkModeEnabled)
{
// BackgroundImage changes may affect rendering logic,
// so we manually update the OwnerDraw flag to ensure correct visual behavior.
UpdateOwnerDraw();
}
}
}

/// <summary>
/// Defines, whether the control is owner-drawn. Based on this,
/// the UserPaint flags get set, which in turn makes it later
Expand All @@ -161,7 +176,6 @@ private protected override bool OwnerDraw
get
{
if (Application.IsDarkModeEnabled

// The SystemRenderer cannot render images. So, we flip to our
// own DarkMode renderer, if we need to render images, except if...
&& Image is null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,21 +547,6 @@ internal void PaintField(
/// </summary>
internal void PaintImage(PaintEventArgs e, LayoutData layout)
{
if (Application.IsDarkModeEnabled && Control.DarkModeRequestState is true && Control.BackgroundImage is not null)
{
Rectangle bounds = Control.ClientRectangle;
bounds.Inflate(-ButtonBorderSize, -ButtonBorderSize);
ControlPaint.DrawBackgroundImage(
e.GraphicsInternal,
Control.BackgroundImage,
Color.Transparent,
Control.BackgroundImageLayout,
Control.ClientRectangle,
bounds,
Control.DisplayRectangle.Location,
Control.RightToLeft);
}

if (Control.Image is not null)
{
// Setup new clip region & draw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ private Color GetButtonBackColor(PushButtonState state)
return backColor;
}

internal void PaintBackgroundImage(PaintEventArgs e, int borderSize, Rectangle paddedBounds)
{
if (Control.BackgroundImage is not null)
{
Rectangle bounds = paddedBounds;
if (Control.FlatStyle == FlatStyle.Flat)
{
bounds.Inflate(-borderSize - 1, -borderSize - 1);
}
else
{
bounds.Inflate(-1, -1);
}

ControlPaint.DrawBackgroundImage(
e.GraphicsInternal,
Control.BackgroundImage,
Color.Transparent,
Control.BackgroundImageLayout,
paddedBounds,
bounds,
bounds.Location,
Control.RightToLeft);
}
}

internal override void PaintUp(PaintEventArgs e, CheckState state)
{
try
Expand All @@ -93,6 +119,10 @@ internal override void PaintUp(PaintEventArgs e, CheckState state)
Control.Parent?.BackColor ?? Control.BackColor,
GetButtonBackColor(pushButtonState),
_ => PaintImage(e, layout),
_ => PaintBackgroundImage(
e,
Control.FlatAppearance.BorderSize,
Control.ClientRectangle),
() => PaintField(
e,
layout,
Expand Down Expand Up @@ -131,6 +161,10 @@ internal override void PaintDown(PaintEventArgs e, CheckState state)
Control.Parent?.BackColor ?? Control.BackColor,
GetButtonBackColor(PushButtonState.Pressed),
_ => PaintImage(e, layout),
_ => PaintBackgroundImage(
e,
Control.FlatAppearance.BorderSize,
Control.ClientRectangle),
() => PaintField(
e,
layout,
Expand Down Expand Up @@ -169,6 +203,10 @@ internal override void PaintOver(PaintEventArgs e, CheckState state)
Control.Parent?.BackColor ?? Control.BackColor,
GetButtonBackColor(PushButtonState.Hot),
_ => PaintImage(e, layout),
_ => PaintBackgroundImage(
e,
Control.FlatAppearance.BorderSize,
Control.ClientRectangle),
() => PaintField(
e,
layout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void RenderButton(
Color parentBackgroundColor,
Color backColor,
Action<Rectangle> paintImage,
Action<Rectangle> paintBackgroundImage,
Action paintField)
{
ArgumentNullException.ThrowIfNull(graphics);
Expand All @@ -63,6 +64,8 @@ public void RenderButton(
// Draw button background and get content bounds
Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault, backColor);

paintBackgroundImage(paddedBounds);

// Paint image and field using the provided delegates
paintImage(contentBounds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void RenderButton(
Color parentBackgroundColor,
Color backColor,
Action<Rectangle> paintImage,
Action<Rectangle> paintBackgroundImage,
Action paintField);

/// <summary>
Expand Down