Click here to Skip to main content
12,693,704 members (29,276 online)
Click here to Skip to main content
Add your own
alternative version

Stats

110.5K views
1.7K downloads
36 bookmarked
Posted

Header-only GroupBox

, 21 Sep 2001 MIT
Rate this:
Please Sign up or sign in to vote.
Illustrates a simple subclass of a Windows.Forms.GroupBox control to paint only the header
<!-- Download Links --> <!-- Article image -->

Sample Image - headeronlygroupbox.gif

<!-- Main HTML starts here -->

Introduction

This is a simple example of a control derived from a Windows.Forms.GroupBox class, to provide the (now fairly standard) 'boxless' separator, of which there are countless MFC examples out there.

The only thing that you might not have come across before is the use of the ControlPaint class.

HeaderOnlyGroupbox

Firstly, it is used to paint disabled text for the control:

if( Enabled )
{
	Brush br = new SolidBrush( ForeColor );
	e.Graphics.DrawString( Text, Font, br, ClientRectangle, format );
	br.Dispose();
}
else
{
	ControlPaint.DrawStringDisabled( e.Graphics, Text, Font, BackColor,
	                                 ClientRectangle, format );
}

Then, it is used again to get the Dark and LightLight control colors, relative to the current BackColor:

Pen forePen = new Pen( ControlPaint.LightLight( BackColor ), SystemInformation.BorderSize.Height );
Pen forePenDark = new Pen( ControlPaint.Dark( BackColor ), SystemInformation.BorderSize.Height );

That's pretty much all there is to it.

Using the class

In your own code it is just a drop-in replacement for GroupBox

If you're using the VS.NET IDE it isn't quite so simple (at least, not for me). I can't find a way to persuade the ToolBox to recognize the control as something it understands. If anyone has any ideas, then drop me a line.

However, it is easy enough to work around. Just drop a regular GroupBox down on your form. Then, switch to the code editor, and replace the 2 instances of GroupBox with HeaderGroupBox (not forgetting to qualify the namespace appropriately). You can carry on using the Forms editor, and it picks up our new rendering just fine.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Matthew Adams
Founder Endjin Ltd
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralNice control but Pin
Xmen W.K.16-Feb-11 0:42
memberXmen W.K.16-Feb-11 0:42 
GeneralBold font Pin
fredgate31-Aug-05 13:47
memberfredgate31-Aug-05 13:47 
GeneralProblem with Control Style Pin
phanf14-Apr-05 5:51
memberphanf14-Apr-05 5:51 
GeneralDesign Mode Pin
John Mancini24-Mar-03 10:56
memberJohn Mancini24-Mar-03 10:56 
GeneralRe: Design Mode Pin
miliu11-Sep-03 17:38
sussmiliu11-Sep-03 17:38 
GeneralWhy not ControlPaint.DrawBorder3D Pin
sentenza15-Jan-03 17:50
membersentenza15-Jan-03 17:50 
GeneralRe: Why not ControlPaint.DrawBorder3D Pin
Matthew Adams16-Jan-03 0:46
memberMatthew Adams16-Jan-03 0:46 
QuestionBalloon Tips? Pin
Iripa30-Nov-01 15:11
memberIripa30-Nov-01 15:11 
AnswerRe: Balloon Tips? Pin
Matthew Adams14-Feb-02 13:25
memberMatthew Adams14-Feb-02 13:25 
GeneralRe: Balloon Tips? Pin
Iripa15-Feb-02 16:51
memberIripa15-Feb-02 16:51 
GeneralGreat! Pin
James T. Johnson30-Oct-01 22:30
memberJames T. Johnson30-Oct-01 22:30 
GeneralThanks, Matt! Pin
Chris Sells23-Sep-01 10:42
memberChris Sells23-Sep-01 10:42 
GeneralRe: Thanks, Matt! Pin
Matthew Adams23-Sep-01 11:15
memberMatthew Adams23-Sep-01 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 22 Sep 2001
Article Copyright 2001 by Matthew Adams
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid