A panel control with positionable captions and icon for Windows Forms






4.93/5 (23 votes)
A panel control with caption support, which can also dock child controls properly.
Introduction
The .NET SDK and some great developers have already implemented panel control(s). What makes my control different is the thing that it is able to dock child controls properly. I tried using similar controls in one of my projects, and found that the caption header was disappearing the moment I changed the docking style of child controls to 'Fill
'. This forced me to capture the 'Resize
' event and then adjust the child control bounds. Six years back, I did develop such a control using VB6 (which is still available for download at VBAccelerator.com). So I decided to redo the same, but this time using C#.
The Control's GUI
Figure 1
Figure 1 shows the basic layout for this control. The area marked with the text "Display Area" denotes the maximum available space for the child control.
Control Properties
The control has the following extra properties apart from those of the base Panel
control:
GradientStart
: The startingColor
for the control's gradient background. The default value isKnownColor.Window
.GradientEnd
: The endColor
for the control's gradient background. The default value isKnownColor.Window
.GradientDirection
: The gradient direction as defined by theLinearGradientMode
enumeration. The default value isLinearGradientMode.Vertical
.BorderStyle
: The border style of the control. The default value isShadow
. The available values are:
None
- Do not draw the border.Single
- Draws a single pixel color border with the specifiedBorderColor
.Shadow
- Draws a single pixel border with the specifiedBorderColor
and adds a shadow around it.Inset
- Draws a border to create a sunken control appearance.Outset
- Draws a border to create a raised control appearance.Groove
- Draws a border to create a sunken border appearance.Ridge
- Draws a border to create a raised border appearance.
BorderColor
: TheColor
to be used for drawing the control border. The default value isKnownColor.ActiveCaption
.CaptionText
: The text to be displayed in the header. The default value is "HeaderPanel".TextAntialias
: A boolean value which determines whether each character in the caption header is to be drawn using the antialiased glyph bitmap. The default value istrue
.CaptionVisible
: A boolean value indicating whether the caption header is visible or not. The default value istrue
.CaptionHeight
: The height in pixels of the caption header. The default value is '24' pixels.CaptionBeginColor
: The startingColor
for the caption's gradient background. The default value isKnownColor.InActiveCaption
.CaptionEndColor
: The endColor
for the caption's gradient background. The default value isKnownColor.ActiveCaption
.CaptionGradientDirection
: The gradient direction as defined by theLinearGradientMode
enumeration. The default value isLinearGradientMode.Vertical
.CaptionPosition
: The position of the caption. The valid values are:
Top
: Display caption panel at the top.Bottom
: Display caption panel at the bottom.Left
: Display caption panel at the left.Right
: Display caption panel at the right.
PanelIcon
: The icon to be displayed in the caption header.PanelIconVisible
: The boolean value indicating whether the caption icon is visible or not. The default value isfalse
.
Child control docking
As I mentioned earlier, with other similar controls, when the Dock
property of the child control is changed to 'Fill
', the child control covers the entire client area of the panel control. This was not the behavior I was expecting. To overcome this, I had multiple choices.
- The first and very oblivious one was to add a
Picturebox
control and set itsDock
property to 'Top
'. But somehow, I did not like this idea. - The second idea that came to my mind was to somehow alter the
ClientArea
of the control, and then capture theWM_NC*
messages to paint the non-client area. It turned out that theClientRectangle
property isreadonly
, and intercepting theWM_NC*
messages requires significant amount of code. So, this was the last choice I reserved to achieve this functionality.
The MSDN Library
Searching the MSDN documentation revealed that every Windows control exposes a property called DisplayRectangle
. Further digging in to the documentation, I came to know that the DisplayRectangle
property returns the client rectangle of the display area of the control. For the base control class, this is equal to the client rectangle. However, inheriting controls might want to change this if their client area differs from their display area. The display rectangle is the smallest rectangle that encloses a control, and is used to lay out controls. This was it. The simple solution I was looking for.
Similar Controls
- Nice Panel: A very nice control from Pure Components. Also supports footer.
- Extended .NET Controls: A very nice control from Hooyberghs Johnny.
- GroupControl: This Group control provides you with two distinct advantages when compared with the standard control: it provides automatic content scrolling, and customizable header location.
Missing Features
Here is a brief list of some additional features I would like to incorporate. (All suggestions are welcomed.)
Customizable header location, a feature found in GroupControl.- Customizable shadow width and colors.
- Auto-adjust for
CaptionHeight
based on the current font and icon dimensions.
Version History
- May 26, 2006: First release.
- May 31, 2006: Second release. The
Caption
can now be displayed at any of the four positionsTop
,Bottom
,Left
, andRight
. TheCaption
'sFont
andHeight
is now defaulted using theSystemInformation
class. - June 09, 2006: Third release. Added support for more border styles. Corrected the resize error pointed out by Mr. leonardas.
- June 26, 2006: Fixed the bug reported by luyuxun. The control now intercepts the
WM_NCxxxx
messages to adjust the client and non-client areas. The credit goes to Mr. Szymon Kobalczyk.