Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("User32.dll")]

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);
g.Flush();
ReleaseDC(m.HWnd, hdc);
}

}

}
Posted
Updated 16-Apr-18 1:24am

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.
 
Share this answer
 
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[^]
 
Share this answer
 
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900