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.