Click here to Skip to main content
15,885,743 members
Articles / Programming Languages / C#
Article

Microsoft Outlook Style Headers

Rate me:
Please Sign up or sign in to vote.
3.27/5 (15 votes)
9 Dec 20021 min read 83.1K   1K   34   1
Creating Panel Effect Label like Outlook

Sample Image - HeaderLabel.jpg

Introduction

Have you ever wondered how in C# to get borders onto visual objects; coming from a Delphi background I was very familiar with having visual controls that offered border effects such as "Etched" or "Sunken" amongst many others. Well, the .NET framework does have this functionality, you just have to impliment it yourselves. The following article will show you how I have taken a simple label object, and applied this logic to obtain a component that offers me the standard microsoft look and feel from programs like Outlook.

Details

Ok, so lets just take a look at the code. Firstly, I need some local properties to allow any of this drawing to take place, I add a visual property called BorderLine, and when set to true, the OnPaint will do our desired drawing, also making use of the so far unused Border3DStyle Enumeration. We set up a property of this type and then set it to Etched as default.

C#
private bool borderLine = true;
private Border3DStyle borderLineStyle = Border3DStyle.Etched;

We then offer the functionality assigned to these two new properties, so that when changed either at runtime or in the ide, they trigger all neccessary redrawing to be done.
C#
[
Category("Appearance"),
Description("Set to True to work with BorderLineStyle" + 
    " To create border effect.")
]
public bool BorderLine {
  get {
    return borderLine;
  }

  set {
    if( value != borderLine ) {
      borderLine = value;
      Invalidate();
    }
  }
}

[
Category("Appearance"),
Description("Add a 3D Style border effect")
]
public Border3DStyle BorderLineStyle {
  get {
    return borderLineStyle;
  }
  set {
    if( value != borderLineStyle) {
      borderLineStyle = value;
      Invalidate();
    }
  }
}

Finally, the Invalidate(); will cause the OnPaint event to be called within the control.
C#
protected override void OnPaint(PaintEventArgs pe) 
{
  base.OnPaint(pe);      
      
  if (borderLine) 
  {
    ControlPaint.DrawBorder3D(pe.Graphics,ClientRectangle.Left+1,
        ClientRectangle.Top +1, 
ClientRectangle.Width -1, 
ClientRectangle.Height -1, 
borderLineStyle);        
  }  
}

Conclusion

This is the real crux of the whole thing, there is a method called DrawBorder3D that accepts the client area to use, and a Border3DStyle to draw on it.

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


Written By
Web Developer
United Kingdom United Kingdom
Does anyone actually read this ? ok, if you do, I used to be a general manager of a computer software company, I developed the applications in Delphi C/S (all versions 2,3,4,5,6). I now am a senior developer for a software company, the language of choice being C#. Although I am a beginner, I hope to become more preficiant in this language as the time goes by.

Cheers for reading if in fact you did actually bother.

Comments and Discussions

 
GeneralAlways include the entire solution, please. Pin
loser_boy_8347514-Oct-05 11:37
loser_boy_8347514-Oct-05 11: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.