Click here to Skip to main content
14,983,121 members
Articles / Desktop Programming / Windows Forms
Posted 26 May 2006


148 bookmarked

A panel control with positionable captions and icon for Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.93/5 (24 votes)
26 Jun 2006CPOL5 min read
A panel control with caption support, which can also dock child controls properly.

Sample Image - HeaderPanel.gif


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 So I decided to redo the same, but this time using C#.

The Control's GUI

Control layout

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 starting Color for the control's gradient background. The default value is KnownColor.Window.
  • GradientEnd: The end Color for the control's gradient background. The default value is KnownColor.Window.
  • GradientDirection: The gradient direction as defined by the LinearGradientMode enumeration. The default value is LinearGradientMode.Vertical.
  • BorderStyle: The border style of the control. The default value is Shadow. The available values are:
    • None - Do not draw the border.
    • Single - Draws a single pixel color border with the specified BorderColor.
    • Shadow - Draws a single pixel border with the specified BorderColor 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: The Color to be used for drawing the control border. The default value is KnownColor.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 is true.
  • CaptionVisible: A boolean value indicating whether the caption header is visible or not. The default value is true.
  • CaptionHeight: The height in pixels of the caption header. The default value is '24' pixels.
  • CaptionBeginColor: The starting Color for the caption's gradient background. The default value is KnownColor.InActiveCaption.
  • CaptionEndColor: The end Color for the caption's gradient background. The default value is KnownColor.ActiveCaption.
  • CaptionGradientDirection: The gradient direction as defined by the LinearGradientMode enumeration. The default value is LinearGradientMode.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 is false.

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.

  1. The first and very oblivious one was to add a Picturebox control and set its Dock property to 'Top'. But somehow, I did not like this idea.
  2. The second idea that came to my mind was to somehow alter the ClientArea of the control, and then capture the WM_NC* messages to paint the non-client area. It turned out that the ClientRectangle property is readonly, and intercepting the WM_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.)

  1. Customizable header location, a feature found in GroupControl.
  2. Customizable shadow width and colors.
  3. Auto-adjust for CaptionHeight based on the current font and icon dimensions.

Version History

  1. May 26, 2006: First release.
  2. May 31, 2006: Second release. The Caption can now be displayed at any of the four positions Top, Bottom, Left, and Right. The Caption's Font and Height is now defaulted using the SystemInformation class.
  3. June 09, 2006: Third release. Added support for more border styles. Corrected the resize error pointed out by Mr. leonardas.
  4. 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prasad Khandekar
Software Developer (Senior) Freelancer
India India
I am a software professional with over 20 years of commercial business applications design and development experience.

My programming experience includes Java, Spring, .NET, Classic VB & ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.

From last 11 years I am mostly working with Java Technology. I am currently available to take up new assignments.

Comments and Discussions

BugBorder not refreshing while resizing. Pin
priyamtheone10-Oct-15 3:55
Memberpriyamtheone10-Oct-15 3:55 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey21-Feb-12 0:22
professionalManoj Kumar Choubey21-Feb-12 0:22 
GeneralIni4j to read and write Pin
Marlys.Jeevana17-Nov-08 4:38
MemberMarlys.Jeevana17-Nov-08 4:38 
GeneralRe: Ini4j to read and write Pin
Prasad Khandekar21-Feb-17 3:30
professionalPrasad Khandekar21-Feb-17 3:30 
GeneralDocking problem Pin
Ilango.M16-Jan-08 7:16
MemberIlango.M16-Jan-08 7:16 
GeneralRe: Docking problem Pin
tommmmmy27-Dec-16 22:01
Membertommmmmy27-Dec-16 22:01 
QuestionHow to get mouse message in the capton?? Pin
sharpturbo16-Sep-07 16:05
Membersharpturbo16-Sep-07 16:05 
Generalshadow behaviour Pin
BobishKindaGuy26-Jun-07 12:05
MemberBobishKindaGuy26-Jun-07 12:05 
Questiondocking panel Pin
xall8-Nov-06 6:34
Memberxall8-Nov-06 6:34 
GeneralAnchor bug Pin
nuhi3-Nov-06 9:13
Membernuhi3-Nov-06 9:13 
GeneralImage Button Pin
rahuljosh007200030-Oct-06 19:41
Memberrahuljosh007200030-Oct-06 19:41 
Generalrounded corners Pin
TheCardinal21-Jul-06 5:03
MemberTheCardinal21-Jul-06 5:03 
GeneralClient Size Pin
Amadrias4-Jul-06 1:20
MemberAmadrias4-Jul-06 1:20 
AnswerRe: Client Size Pin
Prasad Khandekar5-Jul-06 5:21
professionalPrasad Khandekar5-Jul-06 5:21 
GeneralSome Defect Pin
luyuxun18-Jun-06 20:26
Memberluyuxun18-Jun-06 20:26 
AnswerRe: Some Defect Pin
Prasad Khandekar19-Jun-06 6:11
professionalPrasad Khandekar19-Jun-06 6:11 
GeneralSuggestion Pin
BillWoodruff2-Jun-06 12:54
mveBillWoodruff2-Jun-06 12:54 
Generaluseful ! Pin
BillWoodruff1-Jun-06 9:21
mveBillWoodruff1-Jun-06 9:21 
Generalbug on sizing Pin
leonardas31-May-06 22:32
Memberleonardas31-May-06 22:32 
QuestionRe: bug on sizing Pin
Prasad Khandekar1-Jun-06 0:02
professionalPrasad Khandekar1-Jun-06 0:02 
AnswerRe: bug on sizing [modified] Pin
leonardas1-Jun-06 5:29
Memberleonardas1-Jun-06 5:29 
AnswerRe: bug on sizing [modified] Pin
Prasad Khandekar9-Jun-06 2:25
professionalPrasad Khandekar9-Jun-06 2:25 
GeneralSuggestion Pin
leppie26-May-06 6:54
Memberleppie26-May-06 6:54 
GeneralRe: Suggestion Pin
Prasad Khandekar30-May-06 20:37
professionalPrasad Khandekar30-May-06 20:37 

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.