Click here to Skip to main content
13,899,785 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to change the background color of titlebar in winform

What I have tried:

I have used this code to set graphic on form but when I am doing alt+tab for moving to another window then graphic will remove from the form

static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);


private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref System.Windows.Forms.Message m)
const int WM_NCPAINT = 0x85;
base.WndProc(ref m);

if (m.Msg == WM_NCPAINT)

IntPtr hdc = GetWindowDC(m.HWnd);
if ((int)hdc != 0)
Graphics g = Graphics.FromHdc(hdc);
Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle);
int titleHeight = screenRectangle.Top - this.Top;
Rectangle rect = new Rectangle(0, 0, this.Width-100, 30);
g.FillRectangle(Brushes.Green, rect);
ReleaseDC(m.HWnd, hdc);


Updated 16-Apr-18 1:24am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To be honest, since Win8 at least you probably can't do it without removing all the title bar gubbins completely and recreating it yourself. The problem is that the title bar has two states Active and Inactive, and it's bar colours are controlled by that. You can do it in a UWP app via the ApplicationViewTitleBar Class[^] but I don't think it's going to be possible in Winforms.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The "simplest" way is to override WndProc method[^], see: c# - Changing the color of the title bar in WinForm[^]

For further details, please see: Winforms SkinFramework[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I have tried this to put graphic on a form titlebar but when I am doing alt+tab the graphic will be removed from that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190306.1 | Last Updated 20 Jun 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100