4

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?

Matt
  • 25,467
  • 18
  • 120
  • 187
johnnie15
  • 51
  • 1
  • 5
  • 1
    Unless I'm mistaken, I believe that Windows 10's dark theme only affects UWP applications. If you're using WinForms, you'll probably have to [do it yourself](https://stackoverflow.com/q/11862315/4934172). – 41686d6564 stands w. Palestine May 26 '19 at 09:22
  • 1
    Alternatively, you / your users may use [this option](https://i.postimg.cc/mkdxd3bw/Annotation-2019-05-26-113123.png) in Windows 10 settings which allows the selected color to apply to the title bars of all applications. – 41686d6564 stands w. Palestine May 26 '19 at 09:33
  • Thanks! Additionally I decided to make the border hide-able with a tickbox (show to resize and move, hide to use): private void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (System.Windows.Forms.FormBorderStyle.Sizable == this.FormBorderStyle) this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; else this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable; } – johnnie15 May 28 '19 at 03:29

2 Answers2

2

You need to invoke DwmSetWindowAttribute() from dwmapi.dll.

[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void DwmSetWindowAttribute(IntPtr hwnd, 
                                                DWMWINDOWATTRIBUTE attribute, 
                                                ref int pvAttribute, 
                                                uint cbAttribute);

And also DWMWINDOWATTRIBUTE

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 = 20,
    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

var preference = Convert.ToInt32(true);
DwmSetWindowAttribute(this.Handle, 
                      DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, 
                      ref preference, sizeof(uint));

and the title bar of your form will be dark.

Here's Screenshot. Sorry for the mica transparency of windows 11.

enter image description here

  • How do you obtain the right `Handle`? – Matt Oct 04 '22 at 07:32
  • 1
    This handle is the handle of your form. Or you can write `this.Handle` – Mubarrat Hasan Oct 08 '22 at 15:53
  • `Handle` comes automatically from the `Control` and the `Form` also inherits `Control`. – Mubarrat Hasan Oct 09 '22 at 19:02
  • Thank you for the hint, I updated your answer (this.Handle). – Matt Oct 10 '22 at 06:58
  • I tried it with this modification - it compiles and runs, but doesn't create a dark color in the title. – Matt Oct 10 '22 at 08:02
  • Check your windows version it must be greater than or equal to 1903. Or you also enabled "Show accent color on title bars and windows borders" in Settings > Personalization > Colors. It should be disabled. – Mubarrat Hasan Oct 15 '22 at 17:52
  • Thank you for the hint! I verified that I have a newer Windows. In my case it was really the setting you mentioned in Colors, disabling "Show accent color on the following surfaces -> Title bars and windows borders" did the trick! I wouldn't have looked there, it is not obvious :-) – Matt Oct 17 '22 at 06:55
0

There is a DarkUI library available in GitHub, it is described here:

How to use a DarkUI dark user interface in WinForms C#

It provides "dark versions" of the common controls too.

For the title bar, try this: Change the color of the title bar

Maybe it is also a sufficient solution to let the user set up high contrast settings in Windows Personalization (right click the desktop to get there).

Matt
  • 25,467
  • 18
  • 120
  • 187
  • High DPI scale doesn't work in DarkUI library(((((((( – Andrei Krasutski Jan 21 '22 at 19:20
  • @Andrei Krasutski are you sure? I am using DarkUI and get a high DPI just by calling SetProcessDPIAware(); – 8vtwo Mar 30 '22 at 23:01
  • @AndreiKrasutski: Do you mean this API function? [https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiaware](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiaware) – Matt Mar 31 '22 at 06:36
  • @Matt I mean, this is [High DPI support in Windows Forms](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms?view=netframeworkdesktop-4.8) – Andrei Krasutski Mar 31 '22 at 07:55
  • @AndreiKrasutski - Good to know, thanks, so most of it is to be defined in the manifest. – Matt Mar 31 '22 at 14:28