Changing the color of the title bar in WinForm

C#.NetWinforms

C# Problem Overview


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
         |                          |
         |                          |
         |                          |
         |__________________________|

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAravindView Question on Stackoverflow
Solution 1 - C#AravindView Answer on Stackoverflow
Solution 2 - C#Asif MushtaqView Answer on Stackoverflow
Solution 3 - C#user11462042View Answer on Stackoverflow
Solution 4 - C#Cody GrayView Answer on Stackoverflow