62

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
         |                          |
         |                          |
         |                          |
         |__________________________|
Braiam
  • 1
  • 11
  • 47
  • 78
Aravind
  • 1,055
  • 2
  • 11
  • 15
  • Do you mean the [title bar](http://en.wikipedia.org/wiki/Title_bar) or the title bar text? – matthewr Aug 08 '12 at 10:15
  • 3
    yes i want to change the title bar backcolor... – Aravind Aug 08 '12 at 10:58
  • 3
    NOTE: depending on the version of OS, and the user's color scheme, this may be a really bad idea. At minimum, consider color schemes where light and dark are reversed, and decide how you will adapt your background color to fit in with such schemes. – ToolmakerSteve Jan 04 '16 at 22:01
  • 1
    Does this answer your question? [WinForms Dark title bar on Windows 10](https://stackoverflow.com/questions/57124243/winforms-dark-title-bar-on-windows-10) – Matt Oct 29 '21 at 14:14
  • 1
    @Matt That's Win10-specific. This is not (and the solutions for each won't work for the others). – TylerH Dec 10 '21 at 19:06

5 Answers5

26

I solved this problem. This is the code:

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    const int WM_NCPAINT = 0x85;
    if (m.Msg == WM_NCPAINT)
    {
        IntPtr hdc = GetWindowDC(m.HWnd);
        if ((int)hdc != 0)
        {
            Graphics g = Graphics.FromHdc(hdc);
            g.FillRectangle(Brushes.Green, new Rectangle(0, 0, 4800, 23));
            g.Flush();
            ReleaseDC(m.HWnd, hdc);
        }
    }
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Aravind
  • 1,055
  • 2
  • 11
  • 15
  • 5
    Tried on Windows 7. The problems are that: the green rectangle hides form title and minimize/maximize/close bottons; doesn't paint left, right and bottom borders; borders size depends on the specific Windows version. Anyway it's an interesting attempt! – bluish Nov 14 '13 at 14:27
  • 18
    doesn't work for me in Windows 10 64bit, anyone have workaround for this? – Anirudha Gupta May 17 '18 at 06:49
  • 1
    I guess for the same reason but it doesn't work on Windows 11 either – russelrillema May 05 '22 at 09:54
14

What you can do is set the FormBorderStyle property to None and do what ever you want with the form using GDI.

bluish
  • 26,356
  • 27
  • 122
  • 180
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • 6
    That removes everything from the form, leaving a filled rectangle. Then everything is up to you to implement - you start completely from scratch. Maybe good for a game design running in fullscreen, but useless for anything else. – Matt Oct 29 '21 at 13:18
5

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.

Jonas
  • 1,105
  • 1
  • 15
  • 21
  • 1
    I have tried this code on a Framework and a Core project and nothing happens. Running Windows 10.0.17763 Build 17763. Is there a setting I need? – sinDizzy Jun 19 '22 at 20:10
  • 1
    @sinDizzy You will need to add an `app.manifest` file to your project and uncomment the `` for windows 10 as an example – Jonas Jun 20 '22 at 21:04
  • Hello @Jonas, your example works but if user goes on `Settings -> Personalization -> Colors` and choose an accent color and then select to show accent color on `Title bars and window borders`, window comes with this chosen accent color!!! Is there any way to "override" that to? Thank you for your time!!! – Simos Sigma Jan 17 '23 at 09:39
-5

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

-57

This is easy to do:

  1. Right-click on the desktop, and select "Personalize".

  2. Click on the "Window Color" tile at the bottom of the screen.

  3. Choose your new color.

    If your computer is configured to use the Aero theme, you can choose from one of the standard colors or mix one of your own.

    If you're using the Classic theme, you'll see a "Window Color and Appearance" dialog you can use to set colors. Click on the title bar the sample desktop, the one called "Active Window", and then use the "Color 1" and "Color 2" drop-down boxes to pick a new color.

I can only assume this is what you meant, because there is absolutely no excuse to change only the color of your application's title bar. There's a reason that this is a system-wide setting.

Always obey the user's preferences. If they wanted your title bar to be a different color, they would choose a different color.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 81
    -1: When a programmer wants to do something, he/she has its reasons. The answer cannot be "there is no excuse to want to do that". – ThunderGr Sep 02 '14 at 13:04
  • 5
    The "reasons" he/she/it has are wrong or misguided. – Cody Gray - on strike Sep 03 '14 at 03:28
  • 48
    This is not for you to judge. You are either to help or not help. You can post a comment on his question to tell him your opinion. Your judgement is *not* a helpful answer. – ThunderGr Sep 04 '14 at 13:19
  • 11
    This is not an answer to the question. (I agree that it is a really bad idea to do what the question asks, but that is a different discussion. Or as pointed out, is a comment, not an answer.) – ToolmakerSteve Jan 04 '16 at 21:55
  • 9
    Organization/client requirements are a reason to do this. Yes, they may be misguided, but they're never wrong, even when they are :) – Craig Brett Sep 01 '16 at 12:45
  • 3
    It must be dark for my application REGARDLESS of system-wide settings. Who are you to tell me how to write software? – EmpathicSage Jun 04 '20 at 20:47
  • I already done this, and for some forms this works, like notepad for example, but for my application I wrote in VS winforms it does not. Any ideas ? – GuidoG Jun 10 '21 at 15:00
  • I realize this is an old post, but just came across it looking to do the same. Sitting here looking at Edge, Explorer, and VS Code... all with different title bar colors... all from the same vendor. Yes, there are valid reasons to customize how your app looks. – bschellekens Apr 06 '23 at 20:20