Changing the color of the title bar in WinForm

Viewed 99553

Is it possible to change the color of the title bar of a WinForm in C#?

          __________________________
         [Form1_______________-|[]|X] <- I want to change the color of this
         |                          |
         |                          |
         |                          |
         |__________________________|
5 Answers

I created a C# class using @Jonas Kohls answer from here

The class works like a charm and makes it easy to work with multiple forms just call DarkTitleBarClass.UseImmersiveDarkMode(Handle, true); in your load method.

I used this when upgrading some old WinForms apps so its WinForm friendly but only tested on win 8,10 and 11

Example image below Winforms .NetCore 6.0

enter image description here

using System.Runtime.InteropServices;

namespace Myapp.Classes
{
internal class DarkTitleBarClass
{
    [DllImport("dwmapi.dll")]
    private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, 
    ref int attrValue, int attrSize);

    private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
    private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;

    internal static bool UseImmersiveDarkMode(IntPtr handle, bool enabled)
    {
        if (IsWindows10OrGreater(17763))
        {
            var attribute = DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
            if (IsWindows10OrGreater(18985))
            {
                attribute = DWMWA_USE_IMMERSIVE_DARK_MODE;
            }

            int useImmersiveDarkMode = enabled ? 1 : 0;
            return DwmSetWindowAttribute(handle, attribute, ref useImmersiveDarkMode, sizeof(int)) == 0;
        }

        return false;
    }

    private static bool IsWindows10OrGreater(int build = -1)
    {
        return Environment.OSVersion.Version.Major >= 10 && Environment.OSVersion.Version.Build >= build;
    }
  }
}

[EDIT] Don't forget to add a app.manifest and uncomment the appropriate supported OS.

If you want to have a title bar different from others, consider getting a Community license for this

I tried once and it worked. I just had to install it and change Form to SfForm

This is the using statement

using Syncfusion.WinForms.Controls;

The references for WinForms are Syncfusion.Core.WinForms and Syncfusion.Shared.Base

Related