Click here to Skip to main content
Licence 
First Posted 26 May 2006
Views 73,008
Bookmarked 139 times

A panel control with positionable captions and icon for Windows Forms

By | 26 Jun 2006 | Article
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Prasad Khandekar

Architect
Fundtech INDIA Ltd.
India India

Member

I am a software professional with over 16 years of commercial business applications design and development experience.
 
My programming experience includes Java, .NET, Classic VB, ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.
 
For last 5 years I am mostly working on Java platform.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey0:22 21 Feb '12  
GeneralIni4j to read and write PinmemberMarlys.Jeevana4:38 17 Nov '08  
GeneralDocking problem PinmemberIlango.M7:16 16 Jan '08  
QuestionHow to get mouse message in the capton?? Pinmembersharpturbo16:05 16 Sep '07  
Generalshadow behaviour PinmemberBobishKindaGuy12:05 26 Jun '07  
Questiondocking panel Pinmemberxall6:34 8 Nov '06  
GeneralAnchor bug Pinmembernuhi9:13 3 Nov '06  
GeneralImage Button Pinmemberrahuljosh007200019:41 30 Oct '06  
Generalrounded corners PinmemberTheCardinal5:03 21 Jul '06  
GeneralClient Size PinmemberAmadrias1:20 4 Jul '06  
AnswerRe: Client Size PinmemberPrasad Khandekar5:21 5 Jul '06  
GeneralSome Defect Pinmemberluyuxun20:26 18 Jun '06  
AnswerRe: Some Defect PinmemberPrasad Khandekar6:11 19 Jun '06  
GeneralSuggestion PinmemberBillWoodruff12:54 2 Jun '06  
Generaluseful ! PinmemberBillWoodruff9:21 1 Jun '06  
Generalbug on sizing Pinmemberleonardas22:32 31 May '06  
QuestionRe: bug on sizing PinmemberPrasad Khandekar0:02 1 Jun '06  
AnswerRe: bug on sizing [modified] Pinmemberleonardas5:29 1 Jun '06  
AnswerRe: bug on sizing [modified] PinmemberPrasad Khandekar2:25 9 Jun '06  
GeneralSuggestion Pinmemberleppie6:54 26 May '06  
GeneralRe: Suggestion PinmemberPrasad Khandekar20:37 30 May '06  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 26 Jun 2006
Article Copyright 2006 by Prasad Khandekar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid