How to change the color of the title bar in c#?

  • Thread starter Jack Charbonneau
  • Start date
J

Jack Charbonneau

I need to cange the color of the title bar depending on
the state of my application. It seems the only way to do
this is to intercept the WM_NCPAINT message and take
responsiblility for drawing the entire bar - close box,
maximize box, etc.

Isn't there some way I can just tell the OS to change the
color without all this extra work?
 
J

Jonny

Sorry I made a mistake, you should intercept the WM_NCPAINT
not WM_PAINT.

regards,

Jonny

Jonny said:
hello, Jack,
You may try set system color to your own when the window is
activated,and
restore the original color when you app's window is deactivated. the example
code is like this:

[DllImport("user32.dll")]
private static extern Int32 GetSysColor(Int32 Index);
[DllImport("user32.dll")]
private static extern bool SetSysColors(int cElements,Int32[]
lpaElements,Int32[] lpaRgbValues);
private const int COLOR_ACTIVECAPTION = 2;
private int old_color ;
private static int[] element = new int[]{COLOR_ACTIVECAPTION};
private static int[] rgb = null;
private static int[] rgb_old = null;

private void Form1_Activated(object sender, System.EventArgs e)
{
if(rgb == null)
{
old_color = GetSysColor(COLOR_ACTIVECAPTION);
rgb = new int[]{Color.White.ToArgb() & 0x00ffffff};
}
bool b = SetSysColors(1,element,rgb);
}

private void Form1_Deactivate(object sender, System.EventArgs e)
{
if (rgb_old == null)
rgb_old = new int[] {old_color};
bool b = SetSysColors(1,element,rgb_old);
}

But as you can see, it's not efficient because the system will broadcast
WM_SYSCOLORCHANGE message and send WM_PAINT to all affected windows, maybe
it's performance is acceptable when there are only a few top-level windows.
You should intercept the WM_PAINT msg and draw the title bar by yourself if
you want better performance.

Note WM_NCPAINT only draw the frame of the window, if you want to draw the
title bar you should intercept the WM_PAINT.

Best regards,

Jonny Yu

Jack Charbonneau said:
I need to cange the color of the title bar depending on
the state of my application. It seems the only way to do
this is to intercept the WM_NCPAINT message and take
responsiblility for drawing the entire bar - close box,
maximize box, etc.

Isn't there some way I can just tell the OS to change the
color without all this extra work?
 
J

Joe White

Not sure if this is what you're looking for, but take a look at calling
the FlashWindow API function via P/Invoke.

This won't let you pick any arbitrary color, but if you're trying to
flash the title bar to get the user's attention, FlashWindow is the way
to go.
 
Joined
May 7, 2008
Messages
1
Reaction score
0
How to change the title bar color in C#??

Hi Jonny

Can you please tell me how I can draw the title bar of windows form in C# by myself as I want to change the color of the title bar of a form using C#

Thanks
Maqsood


Jonny said:
Sorry I made a mistake, you should intercept the WM_NCPAINT
not WM_PAINT.

regards,

Jonny

"Jonny" wrote in message
news:OUJXN%[email protected]...
> hello, Jack,
> You may try set system color to your own when the window is
> activated,and
> restore the original color when you app's window is deactivated. the

example
> code is like this:
>
> [DllImport("user32.dll")]
> private static extern Int32 GetSysColor(Int32 Index);
> [DllImport("user32.dll")]
> private static extern bool SetSysColors(int cElements,Int32[]
> lpaElements,Int32[] lpaRgbValues);
> private const int COLOR_ACTIVECAPTION = 2;
> private int old_color ;
> private static int[] element = new int[]{COLOR_ACTIVECAPTION};
> private static int[] rgb = null;
> private static int[] rgb_old = null;
>
> private void Form1_Activated(object sender, System.EventArgs e)
> {
> if(rgb == null)
> {
> old_color = GetSysColor(COLOR_ACTIVECAPTION);
> rgb = new int[]{Color.White.ToArgb() & 0x00ffffff};
> }
> bool b = SetSysColors(1,element,rgb);
> }
>
> private void Form1_Deactivate(object sender, System.EventArgs e)
> {
> if (rgb_old == null)
> rgb_old = new int[] {old_color};
> bool b = SetSysColors(1,element,rgb_old);
> }
>
> But as you can see, it's not efficient because the system will broadcast
> WM_SYSCOLORCHANGE message and send WM_PAINT to all affected windows, maybe
> it's performance is acceptable when there are only a few top-level

windows.
> You should intercept the WM_PAINT msg and draw the title bar by yourself

if
> you want better performance.
>
> Note WM_NCPAINT only draw the frame of the window, if you want to draw the
> title bar you should intercept the WM_PAINT.
>
> Best regards,
>
> Jonny Yu
>
> "Jack Charbonneau" wrote in message
> news:[email protected]...
> > I need to cange the color of the title bar depending on
> > the state of my application. It seems the only way to do
> > this is to intercept the WM_NCPAINT message and take
> > responsiblility for drawing the entire bar - close box,
> > maximize box, etc.
> >
> > Isn't there some way I can just tell the OS to change the
> > color without all this extra work?
> >
> >

>
>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top