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.
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.
[
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.
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.
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.