And here's the C# equivalent courtesy of
Dean Cleaver
using System;
using System.Drawing;
using System.Windows.Forms;
namespace NetTech.Windows.Forms.Controls
{
public class Label : Control
{
//Implements Label as I expect it to be (more or less...)
private BorderStyle borderStyle = BorderStyle.None;
private ContentAlignment textAlignment = ContentAlignment.TopLeft;
// Accessing the font object of the Base Class throws a nasty exception!
// So, I am using my own font.
private string fontName = "Tahoma";
private int fontSize = 10;
private FontStyle fontStyle = FontStyle.Regular;
public event EventHandler labelClick;
public event PaintEventHandler labelPaint;
public Label()
{
}
public BorderStyle BorderStyle
{
get {return this.borderStyle;}
set {this.borderStyle = value;}
}
public string FontName
{
get {return this.fontName;}
set {this.fontName = value;}
}
public int FontSize
{
get {return this.fontSize;}
set {this.fontSize = value;}
}
public FontStyle FontStyle
{
get {return this.fontStyle;}
set {this.fontStyle = value;}
}
public ContentAlignment TextAlignment
{
get {return this.textAlignment;}
set {this.textAlignment = value;}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
{
Graphics graphics = pe.Graphics;
Pen pen = new Pen(Color.Black);
SolidBrush brush = new SolidBrush(ForeColor);
Font font = new Font(this.fontName, this.fontSize, this.fontStyle);
SizeF s;
if (this.BorderStyle == BorderStyle.FixedSingle)
graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
else if (this.BorderStyle == BorderStyle.Fixed3D)
graphics.DrawRectangle(pen, this.ClientRectangle);
s = graphics.MeasureString(this.Text, font);
switch (this.textAlignment)
{
case ContentAlignment.TopCenter:
graphics.DrawString(this.Text, font, brush, (this.Width - s.Width) / 2,
(this.Height - s.Height) / 2);
break;
case ContentAlignment.TopLeft:
graphics.DrawString(this.Text, font, brush, 2, (this.Height - s.Height)
/ 2);
break;
case ContentAlignment.TopRight:
graphics.DrawString(this.Text, font, brush, this.Width - s.Width - 2,
(this.Height - s.Height) / 2);
break;
}
pen.Dispose();
brush.Dispose();
font.Dispose();
if (labelPaint != null)
labelPaint(this, pe);
base.OnPaint(pe);
}
protected override void OnClick(System.EventArgs e)
{
if (labelClick != null)
labelClick(this, e);
base.OnClick(e);
}
}
}