Skip to content

Conversation

LeafShi1
Copy link
Member

@LeafShi1 LeafShi1 commented Aug 20, 2025

Fixes #13823

Root Cause

The OwnerDraw property of the Button control incorrectly returns false when BackgroundImage is set during early initialization. This is due to the OwnerDraw logic checking BackgroundImage == null before the control's handle is created, which causes a premature fallback to the system renderer. As a result, even though BackgroundImage is later assigned, it is ignored during rendering in FlatStyle.Standard mode.

Proposed changes

  • Invoking UpdateOwnerDraw in BackgroundImage property of Button.cs to update the OwnerDraw flag to ensure correct visual behavior in Standard mode
  • Place the DrawButtonBorder method after PaintBackgroundImage.
  • Recalculate the background image drawing area.
  • Remove the border rounding logic for flat mode to ensure that the border completely surrounds the button (in classic mode, flat mode buttons also do not have rounded corners).

Customer Impact

  • The BackgroundImage of Button can be displayed when the FlatStyle is Standard in DrakMode

Regression?

  • Yes

Risk

  • Minimal

Screenshots

Before

The BackgroundImage of Button is not displayed when the FlatStyle is Standard in DrakMode
image

After

The BackgroundImage of Button displayed when the FlatStyle is Standard in DrakMode
image

Test methodology

  • Manually

Test environment(s)

  • .net 10.0.0-rc.1.25418.105
Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where Button controls with BackgroundImage fail to render properly in dark mode when using FlatStyle.Standard. The problem occurs due to premature evaluation of the BackgroundImage property during early initialization before the control's handle is created.

  • Adds an IsHandleCreated check to the OwnerDraw property logic
  • Delays the BackgroundImage null check until after handle creation
  • Ensures buttons with BackgroundImage properly use custom dark mode rendering

Copy link

codecov bot commented Aug 20, 2025

Codecov Report

❌ Patch coverage is 0% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.11923%. Comparing base (5dcf17d) to head (a6d78ba).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #13824         +/-   ##
===================================================
- Coverage   77.12069%   77.11923%   -0.00146%     
===================================================
  Files           3273        3274          +1     
  Lines         644919      645070        +151     
  Branches       47693       47703         +10     
===================================================
+ Hits          497366      497473        +107     
- Misses        143849      143939         +90     
+ Partials        3704        3658         -46     
Flag Coverage Δ
Debug 77.11923% <0.00000%> (-0.00146%) ⬇️
integration 18.85980% <0.00000%> (-0.12540%) ⬇️
production 51.94586% <0.00000%> (-0.00925%) ⬇️
test 97.41286% <ø> (+0.00029%) ⬆️
unit 49.42084% <0.00000%> (+0.01500%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KlausLoeffelmann
Copy link
Member

@Epica3055, how is tbis related to the changes for RC2?

@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Sep 5, 2025
@LeafShi1
Copy link
Member Author

LeafShi1 commented Sep 5, 2025

@Epica3055, how is this related to the changes for RC2?

This fix is unrelated to RC2.
The original issue is #13720, but there are two pull requests (PRs) addressing this issue.

  1. Draw background image for button in dark mode #13809 (already in RC2). After testing, this only addresses the issue where the background image doesn't display when the button's FlatStyle is Flat & Popup.
  2. This pull request provides a supplementary fix for the issue where background images do not display in Dark Mode when the button's FlatStyle is set to Standard.

@LeafShi1 LeafShi1 changed the title Add judgement "IsHandleCreated" in OwnerDraw Update the OwnerDraw flag when BackgroundImage changes Sep 5, 2025
@KlausLoeffelmann
Copy link
Member

Makes sense.
Please make sure, we would get the other edge case also taken into account.
@merriemcgaw - this would make very much sense to also take in RC2, if tactics agree.
Argument for late discovery: All edge cases, which CTI found out after implementing recent fixes. All completely scope to dark mode AND to the respective narrow area, and without ANY chance to regress the light mode render path.

@LeafShi1, @Epica3055: If we would get green light for RC2, this would need to be back ported.
All: Before we backport this, let's take @Olina-Zhang another look at it, to see, if we would find more edge cases in this area, so we do not need to go another time to tactics to ask for backporting a fix!

Thanks!

@dotnet-policy-service dotnet-policy-service bot added waiting-author-feedback The team requires more information from the author and removed waiting-author-feedback The team requires more information from the author labels Sep 7, 2025
@LeafShi1
Copy link
Member Author

LeafShi1 commented Sep 8, 2025

@KlausLoeffelmann @merriemcgaw Based on the test results of this PR, I need to confirm should OwnerDraw be enabled in Dark Mode with FlatStyle = Standard?

If OwnerDraw should not be enabled

  • The BackColor, ForeColor, and background image settings for the Button control do not take effect. This mean that the current issue is not a bug, but rather a By Design issue. Please confirm this.
  • A further question is: For other controls (such as Label, GroupBox), color property settings currently take effect in DarkMode. If OwnerDraw is not enabled for Button, this will lead to inconsistent UI styles. Should OwnerDraw be disabled for these controls as well to maintain consistency?
  • Also, it's important to note that in .NET 9.0, color property settings for Button do take effect, so this is currently a regression.

If we decide to enable OwnerDraw to restore the effects of these properties, some existing drawing logic (such as the code at

private protected override bool OwnerDraw =>
// Order is key here - do NOT change!
// We want NO owner draw ONLY when we're
// * in Dark Mode
// * when _then_ the Appearance is Button
// * but then ONLY when we're rendering with FlatStyle.Standard
// (because that would let us usually let us draw with the VisualStyleRenderers,
// which cause HighDPI issues in Dark Mode).
(!Application.IsDarkModeEnabled
|| Appearance != Appearance.Button
|| FlatStyle != FlatStyle.Standard)
&& base.OwnerDraw;
) may need to be adjusted.

@LeafShi1 LeafShi1 closed this Sep 10, 2025
@dotnet-policy-service dotnet-policy-service bot removed the waiting-review This item is waiting on review by one or more members of team label Sep 10, 2025
@LeafShi1 LeafShi1 reopened this Sep 10, 2025
@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Sep 11, 2025
@LeafShi1 LeafShi1 force-pushed the Issue_13823_Add_handleJudgement_in_OwnerDraw branch from e17e417 to 6da3eca Compare September 11, 2025 09:34
@LeafShi1 LeafShi1 force-pushed the Issue_13823_Add_handleJudgement_in_OwnerDraw branch from 6da3eca to 9e82a5f Compare September 11, 2025 09:59
Copy link
Member

@KlausLoeffelmann KlausLoeffelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not holding this off, since it is too niche.
Let's take this as it is, so we can go forward.

@merriemcgaw FYI.

@LeafShi1 LeafShi1 marked this pull request as draft September 25, 2025 06:30
@dotnet-policy-service dotnet-policy-service bot added the draft draft PR label Sep 25, 2025
@LeafShi1 LeafShi1 marked this pull request as ready for review September 25, 2025 06:59
@dotnet-policy-service dotnet-policy-service bot removed the draft draft PR label Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-area-label waiting-review This item is waiting on review by one or more members of team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dark Mode: The BackgroundImage of Button is not displayed when the FlatStyle is Standard in DrakMode
2 participants