How do I change the color of the title bar in a Win Forms .Net app using c#?

We can use the WM_NCPAINT Message to do that. We must have to code for override the WndProc method to change the color of title bar in a Win Forms. Here I have done that with the following code. You can do that by using the code below.

using System.Runtime.InteropServices;

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


That’s it…

…S.VinothkumaR.

2 comments:

Seshu said...

It is showing error here
base.WndProc(ref m);

and error is as follows
system.windows.window does not contain a definition for WndProc

Please help me out with a sample application if u have any such

CS said...

Well how to create a new database file say client001 from a bak file say Sample.bak in Vs2010. my bak file is in sql2008r2. Can u help me out with sample code if any