I have a WinForms application which automatically adjusts to the dark/light theme on Windows 10. My problem is that the title bar of my window always stays white, regardless which theme the user selects.
Top is current, bottom is how I want it (simulated with Photoshop)
See explorer
for example. That is not an UWP app, however it uses a dark title bar on Windows 1903 and newer (when a dark theme is selected).
How can I achieve the same thing? I do not want to use any custom titlebar as I want the application to look and behave like any native application on older Windows versions as well.
3
Answers
So after some long searching, I have finally found the answer for this. The trick is to use
dwmapi.dll
'sDwmSetWindowAttribute
and passing the undocumented constantDWMWA_USE_IMMERSIVE_DARK_MODE
into the function. In C#, the code for this looks a little something like this (works with both WinForms and WPF):The fastest way:
For the solution from Jonas Kohl, remember that for .net fw 4.8.1 and prior, the version returned is not ok, fixed in .Net6, here a snippet (.Net 5 is not managed):