Can't Remove DarkMode Form Theme inside OnHandleCreated event
.NET version
.NET 10.0.100-preview.5.25257.112
Did it work in .NET Framework?
No
Did it work in any of the earlier releases of .NET Core or .NET 5+?
No, also repro in .NET 9.0 which it started to be supported.
Issue description
The Compatible Method to Create Custom Painting On NoneClient Area of Form is To Remove Form Theme inside OnHandleCreated event this option is Not Supported when using DarkMode
Steps to reproduce
1- Create a winforms .NET Core project . 2- Enable DarkMode by Application.SetColorMode(SystemColorMode.Dark) in Program.cs file. 3- override void OnHandleCreated event to remove The Theme
protected override void OnHandleCreated(EventArgs e)
{
PInvoke.SetWindowTheme(Handle,"","");
}
https://github.com/user-attachments/assets/93fd714b-9c65-4be2-91ea-a32a38fc0965
Please take a look at: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.controlstyles?view=windowsdesktop-9.0
There is a new style introduced in .NET 9:
Does this address your issue? If you want a control, which is derived from one which is set to opt in for dark mode, to opt out, clearing this flag should do the trick. If it does not, please file a bug!
If yes, close this please. Thanks for pointing this out - we should probably do an additional DarkMode blog post (@merriemcgaw FYI) mentioning all those subtle details.
If this does NOT address your issue, let's see, what the best solution is not to block anybody, so we can get this in for Preview 6.
Thanks so much for your engagement!!
Please take a look at: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.controlstyles?view=windowsdesktop-9.0
There is a new style introduced in .NET 9:
Does this address your issue? If you want a control, which is derived from one which is set to opt in for dark mode, to opt out, clearing this flag should do the trick. If it does not, please file a bug!
Hi @KlausLoeffelmann,
Thank you for your detailed suggestions and ongoing support.
Unfortunately, none of the proposed solutions have resolved my scenario. The issue with removing the DarkMode form theme inside the OnHandleCreated event still persists. Here is some feedback on the approaches mentioned:
OptimizedDoubleBuffer: This does not address DWM transparency and does not resolve the problem.
ApplyThemingImplicitly: The WinForms Form source code appears not to respect this option. For reference, see Form.cs#L4277. Additionally, could you clarify:
Why is SetWindowTheme being called in the OnLoad event, given that it is already done on the control? What is the rationale for calling SetWindowTheme if the Form or Control is not themed? In my testing, these approaches also did not solve the issue. UseTextForAccessibility: I had not used this before, but after researching, I found this link. It also does not resolve the problem in my case.
In some situations, I need DWM frame transparency, and turning off VisualStyle is currently the best workaround I have found.
For reference, here’s a screenshot illustrating the issue:
https://github.com/user-attachments/assets/3ca3cc45-8682-4313-8a79-944e7a41d843
Sample is attached. WinFormsAppTest.zip
If you have any other ideas or need additional information from my side, please let me know. I appreciate your continued support.
Taking a look next week!
@memoarfaa: Just to keep you posted, I am discussing a slightly different and more discoverable approach to easier set things up. And yes, you are correct: The Form needs to opt out when that flag is reset. Good catch!
@KlausLoeffelmann @JeremyKuhne @merriemcgaw this is my primary use case diagram after testing all DarkMode issues
flowchart TD
%% Main objects
App([Application])
SCM([SystemColorMode])
CA([Client Area Drawing])
NCA([NonClient Area Drawing])
Controls([Controls])
Dialogs([Dialogs])
SystemDialog([SystemDialog])
PopWindows([PopWindows])
Forms([Forms])
SystemMenus([SystemMenus])
Tooltips([Tooltips])
ToolStrips([ToolStrips])
NoteTT(["Requires:<br/>• NonClient Area border color<br/>• DWM round policy"])
App -->|uses| SCM
%% SystemColorMode handles drawing areas
SCM --> CA
SCM --> NCA
%% Client/NonClient areas cover all dark mode objects
CA --> Controls
CA --> Dialogs
CA --> SystemDialog
CA --> PopWindows
CA --> Forms
CA --> SystemMenus
CA --> Tooltips
CA--> ToolStrips
NCA --> Controls
NCA --> Dialogs
NCA --> SystemDialog
NCA --> PopWindows
NCA --> Forms
NCA --> SystemMenus
NCA --> Tooltips
NCA--> ToolStrips
%% PopWindows includes SystemMenus and Tooltips for clarity
PopWindows --> SystemMenus
PopWindows --> Tooltips
PopWindows --> ToolStrips
Tooltips -.-> NoteTT
%% Styling for high contrast (visible on light and dark themes)
classDef actor fill:#1565c0,stroke:#fff,stroke-width:3px,color:#fff;
classDef manager fill:#43a047,stroke:#fff,stroke-width:3px,color:#fff;
classDef area fill:#f8bbd0,stroke:#ad1457,stroke-width:2px,color:#3e2723;
classDef object fill:#fffde7,stroke:#37474f,stroke-width:2px,color:#263238;
classDef subobject fill:#ffe082,stroke:#37474f,stroke-width:2px,color:#263238;
class App actor
class SCM manager
class CA,NCA area
class Controls,Dialogs,SystemDialog,PopWindows,Forms object
class SystemMenus,Tooltips subobject
class NoteTT note