Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A panel control with positionable captions and icon for Windows Forms

0.00/5 (No votes)
26 Jun 2006 1  
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