467,076 Members | 973 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,076 developers. It's quick & easy.

Any way to custom draw a title bar?

How would you go about custom drawing the title bar of a form? I'd like
to change it so that I can add different colors to my title and also
change how the close button works (since I override it and cause the
form to minimize).

Thanks in advance,
Benny
Nov 17 '05 #1
  • viewed: 15372
Share:
2 Replies
Benny,

You will have to override the WndProc method on your form. In there,
you will want to handle the WM_NCPAINT message, which is sent when the frame
(including the title bar) needs to be painted.

If you call the base method of WndProc, it will draw the frame, and then
you can draw over the frame, or, you can paint the whole thing yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
How would you go about custom drawing the title bar of a form? I'd like
to change it so that I can add different colors to my title and also
change how the close button works (since I override it and cause the form
to minimize).

Thanks in advance,
Benny

Nov 17 '05 #2
I'm now overriding wndproc and handling the wm_ncpaint by letting base
do it first and then drawing over the top... This works the first time
the program draws the close button but after that it doesn't work...
Note: I'm just drawing a big white box right now to get the
functionality in - why is the close button drawing outside of
wm_ncpaint?... Here's the code:

protected override void WndProc(ref Message m)
{
switch ( m.Msg )
{
case (int) MSG.WM_NCPAINT:
base.WndProc( ref m );
Main_OnNcPaint( ref m );
return;
}
}

private void Main_OnNcPaint ( ref Message m )
{
Debug.WriteLine("drawing");
// If I use GetDCEx, the program crashes on the Graphics
// .FromHdc Line - GetDCEx returns 0?
//IntPtr hdc = User32.GetDCEx( m.HWnd, m.WParam,
((uint)DCX_FLAGS.DCX_WINDOW)|((uint)DCX_FLAGS.DCX_ INTERSECTRGN) );

IntPtr hdc = User32.GetWindowDC(m.HWnd);
Graphics g = Graphics.FromHdc(hdc);

int CaptionHeight = Bounds.Height - ClientRectangle.Height; //Titlebar
Size CloseButtonSize = SystemInformation.CaptionButtonSize;
int X = Bounds.Width - CloseButtonSize.Width;
int Y = 6;

// ControlPaint causes the program to not use XP styles anymore :(
//ControlPaint.DrawButton(g, X, Y, 15, 15, ButtonState.Normal);

// Fill a rectangle instead
g.FillRectangle(SystemBrushes.Window, X, Y, CloseButtonSize.Width,
CloseButtonSize.Height);
//g.Dispose();
User32.ReleaseDC(m.HWnd, hdc);

m.Result = IntPtr.Zero;

}



Nicholas Paldino [.NET/C# MVP] wrote:
Benny,

You will have to override the WndProc method on your form. In there,
you will want to handle the WM_NCPAINT message, which is sent when the frame
(including the title bar) needs to be painted.

If you call the base method of WndProc, it will draw the frame, and then
you can draw over the frame, or, you can paint the whole thing yourself.

Hope this helps.

How would you go about custom drawing the title bar of a form? I'd like
to change it so that I can add different colors to my title and also
change how the close button works (since I override it and cause the form to minimize).

Thanks in advance,
Benny

Nov 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Florida Draw@FreeBSD.csie.NCTU.edu.tw, | last post: by
reply views Thread by Steve | last post: by
5 posts views Thread by Brian Keating EI9FXB | last post: by
3 posts views Thread by Angapparaj K via .NET 247 | last post: by
15 posts views Thread by Tinus | last post: by
4 posts views Thread by Alvo von Cossel I | last post: by
2 posts views Thread by Furer Ramon | last post: by
2 posts views Thread by eljainc | last post: by
2 posts views Thread by ahmed.maryam@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.