I have downloaded the Windows 10 update that includes the dark theme.
File explorer et al are in dark theme, but when I create my own C# form application, the title bar is bright white.
How can I make my own desktop apps follow the dark theme that I have set in Windows?
You can use the following code to invoke DwmSetWindowAttribute() from dwmapi.dll in your .NET application.
DwmSetWindowAttribute FunctionFirst, import the DwmSetWindowAttribute function from dwmapi.dll.
You can use CsWin32 to platform invoke known as P/Invoke. You need C# 9 to use source generators.
using System;
using System.Runtime.InteropServices;
[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd,
DWMWINDOWATTRIBUTE attribute,
ref int pvAttribute,
uint cbAttribute);
using System;
using System.Runtime.InteropServices;
[LibraryImport("dwmapi.dll")]
public static partial int DwmSetWindowAttribute(IntPtr hwnd,
DWMWINDOWATTRIBUTE attribute,
ref int pvAttribute,
uint cbAttribute);
DWMWINDOWATTRIBUTE EnumNext, define the DWMWINDOWATTRIBUTE enumeration.
public enum DWMWINDOWATTRIBUTE : uint
{
DWMWA_NCRENDERING_ENABLED,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_PASSIVE_UPDATE_MODE,
DWMWA_USE_HOSTBACKDROPBRUSH,
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19,
DWMWA_USE_IMMERSIVE_DARK_MODE,
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR,
DWMWA_TEXT_COLOR,
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS,
DWMWA_SYSTEMBACKDROP_TYPE,
DWMWA_LAST
}
When your form initializes, apply the DWMWA_USE_IMMERSIVE_DARK_MODE attribute to enable the dark mode for the title bar.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var preference = Convert.ToInt32(true);
if (DwmSetWindowAttribute(this.Handle,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
ref preference,
sizeof(uint)) != 0)
Marshal.ThrowExceptionForHR(
DwmSetWindowAttribute(this.Handle,
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
ref preference,
sizeof(uint)));
}
Here is a screenshot demonstrating the dark mode title bar.

When your program runs, apply the Application.SetDefaultDarkMode(DarkMode) to enable the dark mode for the entire application.
[STAThread]
public static void Main()
{
ApplicationConfiguration.Initialize();
// Enable dark mode here
Application.SetDefaultDarkMode(DarkMode.Enabled);
Application.Run(new MainForm());
}
Track Progress: [API Suggestion] Introduce Dark Mode and A11Y compatible Visual Styles for Apps targeting .NET 9 and Win 11
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With