Fix CheckBox/RadioButton Appearance.Button + FlatStyle.Standard invisible in dark mode#14397
Conversation
…ible in dark mode Fixes dotnet#14347 `CheckBox` and `RadioButton` with `Appearance.Button + FlatStyle.Standard` intentionally disabled owner-draw in dark mode to work around a VisualStyleRenderer HighDPI issue: ```csharp private protected override bool OwnerDraw => (!Application.IsDarkModeEnabled || Appearance != Appearance.Button || FlatStyle != FlatStyle.Standard) && base.OwnerDraw; ``` With `OwnerDraw = false`, WinForms delegates painting to the native ComCtl32 button (`BS_3STATE | BS_PUSHLIKE`). The native control renders with light-mode colors (or transparent) on a dark form background, making the controls completely invisible. - **`CheckBox.cs` / `RadioButton.cs`**: Remove the dark mode exception from `OwnerDraw`. Both controls now return `base.OwnerDraw` (i.e., `FlatStyle != FlatStyle.System`) unconditionally, restoring owner-draw for `Appearance.Button + FlatStyle.Standard` in dark mode. - **`CheckBoxStandardAdapter.cs` / `RadioButtonStandardAdapter.cs`**: Change `CreateButtonAdapter()` from `new ButtonStandardAdapter(Control)` to `DarkModeAdapterFactory.CreateStandardAdapter(Control)`. In dark mode this returns `ButtonDarkModeAdapter`, which renders using explicit dark mode colors (`#333333` background, `#9B9B9B` border, `#F0F0F0` text) and does not use VisualStyleRenderers, resolving both the visibility issue and the HighDPI concern that motivated the original workaround. `CheckBox` and `RadioButton` controls with `Appearance.Button + FlatStyle.Standard` are now visible and correctly styled in dark mode. Previously they were completely invisible. No. Minimal. Manual. - 11.0.100-preview.3.26161.119
There was a problem hiding this comment.
Pull request overview
Fixes dark-mode invisibility for CheckBox/RadioButton when using Appearance.Button + FlatStyle.Standard by restoring owner-draw in dark mode and routing “button-like” rendering through a dark-mode-capable adapter.
Changes:
- Remove dark-mode special-casing from
OwnerDrawinCheckBoxandRadioButton(now alwaysbase.OwnerDraw). - Use
DarkModeAdapterFactory.CreateStandardAdapter(...)for theAppearance.Buttonrendering path in the Standard adapters.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/RadioButton.cs | Restores owner-draw in dark mode by removing the special OwnerDraw condition. |
| src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/CheckBox.cs | Restores owner-draw in dark mode by removing the special OwnerDraw condition. |
| src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonStandardAdapter.cs | Routes Appearance.Button painting through the dark-mode adapter factory. |
| src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxStandardAdapter.cs | Routes Appearance.Button painting and sizing through the dark-mode adapter factory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| private protected override bool OwnerDraw => base.OwnerDraw; | ||
|
|
|
|
||
| protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonStandardAdapter(Control); | ||
| protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreateStandardAdapter(Control); | ||
|
|
| private new ButtonBaseAdapter ButtonAdapter => base.ButtonAdapter; | ||
|
|
||
| protected override ButtonBaseAdapter CreateButtonAdapter() => new ButtonStandardAdapter(Control); | ||
| protected override ButtonBaseAdapter CreateButtonAdapter() => DarkModeAdapterFactory.CreateStandardAdapter(Control); | ||
|
|
| || Appearance != Appearance.Button | ||
| || FlatStyle != FlatStyle.Standard) | ||
| && base.OwnerDraw; | ||
| private protected override bool OwnerDraw => base.OwnerDraw; |
|
Should we enable OwnerDraw in Standard mode? @KlausLoeffelmann |
Review Notes for PR #14397Thanks for the fix, @ricardobossan. Making these controls visible in dark mode is important. A few observations: 1. CheckState visual fidelity concernWhen
This could trade the invisibility bug for a subtler visual-state bug. Could you verify that checked and indeterminate states still look correct on hover in dark mode? If 2. Preferred size consistency
3. Missing automated testsThe PR description mentions manual testing only. Since this is a regression fix for a specific combination ( 4. LeafShi1's question@LeafShi1 raised a good question about whether we should enable OwnerDraw in Standard mode — it would be great to get @KlausLoeffelmann's input on that before merging. Overall the approach looks sound. The main risk is point #1 above. (Copilot co-authored on Simon Zhao.) |
|
@SimonZhao888, thanks for the thorough review! 1. CheckState visual fidelity: fixedYou're right. 2. Preferred size consistency: non-issue
3. Automated tests: addedAdded a regression test to both 4. @LeafShi1's questionAgreed. It would be great to get @KlausLoeffelmann's input before merging. He added the original |
Fixes #14347
Root Cause
CheckBoxandRadioButtonwithAppearance.Button + FlatStyle.Standardintentionally disabled owner-draw in dark mode to work around a VisualStyleRenderer HighDPI issue:With
OwnerDraw = false, WinForms delegates painting to the native ComCtl32 button (BS_3STATE | BS_PUSHLIKE). The native control renders with light-mode colors (or transparent) on a dark form background, making the controls completely invisible.Proposed changes
CheckBox.cs/RadioButton.cs: Remove the dark mode exception fromOwnerDraw. Both controls now returnbase.OwnerDraw(i.e.,FlatStyle != FlatStyle.System) unconditionally, restoring owner-draw forAppearance.Button + FlatStyle.Standardin dark mode.CheckBoxStandardAdapter.cs/RadioButtonStandardAdapter.cs: ChangeCreateButtonAdapter()fromnew ButtonStandardAdapter(Control)toDarkModeAdapterFactory.CreateStandardAdapter(Control). In dark mode this returnsButtonDarkModeAdapter, which renders using explicit dark mode colors (#333333background,#9B9B9Bborder,#F0F0F0text) and does not use VisualStyleRenderers, resolving both the visibility issue and the HighDPI concern that motivated the original workaround.Customer Impact
CheckBoxandRadioButtoncontrols withAppearance.Button + FlatStyle.Standardare now visible and correctly styled in dark mode. Previously they were completely invisible.Regression?
No.
Risk
Minimal.
Screenshots
Before
After
Test methodology
Manual.
Test environment(s)
Microsoft Reviewers: Open in CodeFlow