Click here to Skip to main content
Click here to Skip to main content

A panel control with positionable captions and icon for Windows Forms

, 26 Jun 2006
Rate this:
Please Sign up or sign in to vote.
A panel control with caption support, which can also dock child controls properly.

Sample Image - HeaderPanel.gif

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

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.

License

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

Share

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

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey21-Feb-12 0:22 
GeneralIni4j to read and write PinmemberMarlys.Jeevana17-Nov-08 4:38 
GeneralDocking problem PinmemberIlango.M16-Jan-08 7:16 
QuestionHow to get mouse message in the capton?? Pinmembersharpturbo16-Sep-07 16:05 
Generalshadow behaviour PinmemberBobishKindaGuy26-Jun-07 12:05 
Questiondocking panel Pinmemberxall8-Nov-06 6:34 
GeneralAnchor bug Pinmembernuhi3-Nov-06 9:13 
GeneralImage Button Pinmemberrahuljosh007200030-Oct-06 19:41 
Generalrounded corners PinmemberTheCardinal21-Jul-06 5:03 
GeneralClient Size PinmemberAmadrias4-Jul-06 1:20 
AnswerRe: Client Size PinmemberPrasad Khandekar5-Jul-06 5:21 
GeneralSome Defect Pinmemberluyuxun18-Jun-06 20:26 
AnswerRe: Some Defect PinmemberPrasad Khandekar19-Jun-06 6:11 
GeneralSuggestion PinmemberBillWoodruff2-Jun-06 12:54 
Generaluseful ! PinmemberBillWoodruff1-Jun-06 9:21 
Generalbug on sizing Pinmemberleonardas31-May-06 22:32 
QuestionRe: bug on sizing PinmemberPrasad Khandekar1-Jun-06 0:02 
AnswerRe: bug on sizing [modified] Pinmemberleonardas1-Jun-06 5:29 
AnswerRe: bug on sizing [modified] PinmemberPrasad Khandekar9-Jun-06 2:25 
GeneralSuggestion Pinmemberleppie26-May-06 6:54 
GeneralRe: Suggestion PinmemberPrasad Khandekar30-May-06 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140827.1 | Last Updated 26 Jun 2006
Article Copyright 2006 by Prasad Khandekar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid