Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my Form title bar follow the windows dark theme? [duplicate]

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?

like image 882
johnnie15 Avatar asked Sep 16 '25 03:09

johnnie15


1 Answers

Before .NET 10 and other versions

For dark title bar:

You can use the following code to invoke DwmSetWindowAttribute() from dwmapi.dll in your .NET application.

Step 1: Import the DwmSetWindowAttribute Function

First, 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.

Before .NET 7 and other versions

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);

From .NET 7

using System;
using System.Runtime.InteropServices;

[LibraryImport("dwmapi.dll")]
public static partial int DwmSetWindowAttribute(IntPtr hwnd,
                                                DWMWINDOWATTRIBUTE attribute,
                                                ref int pvAttribute,
                                                uint cbAttribute);

Step 2: Define the DWMWINDOWATTRIBUTE Enum

Next, 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
}

Step 3: Apply the Attribute in Your Form

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)));
}

Screenshot Example

Here is a screenshot demonstrating the dark mode title bar.

Dark Mode Title Bar

.NET 10

Make the entire application including the title bar dark: (Expected to come)

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

like image 151
Mubarrat Avatar answered Sep 17 '25 17:09

Mubarrat