Introduction
The xpLabel is a simple label control with an extension that it
has different look-and-feel from traditional label control. It
uses color schemes to make itself look-like some of the aesthetic
label controls available otherwise. However, you can also always
customize the color scheme and your creativity is the limit to it.
Using the code
Lets start analyzing the code. The first thing to consider is the types used.
public enum BackColorSchemeType
{
Office2003Blue,
Office2003Orange,
Office2003Silver,
Office2003Green,
DarkBlueTinge,
OSXLightGray,
LightGreenVikings,
Azure
}
public enum TextAlignStyle
{
TAlignLeftTop,
TAlignLeftMiddle,
TAlignLeftBottom,
TAlignMiddleTop,
TAlignMiddleMiddle,
TAlignMiddleBottom
}
Extra properties that are exposed to client are
[Description("Starting color of Gradient"),
Category("Gradient Colors")]
public Color BackColor1
{
get{ return _bkgcolo1; }
set
{
if( (_bkgcolo1 != value) )
{
_bkgcolo1 = value;
Invalidate();
}
}
}
[Description("Ending color of Gradient"),
Category("Gradient Colors")]
public Color BackColor2
{
get{ return _bkgcolo2; }
set
{
if( (_bkgcolo2 != value) )
{
_bkgcolo2 = value;
Invalidate();
}
}
}
[Description("Show Shadow for the text")]
public bool ShowShadow
{
get{ return _shadow; }
set
{
_shadow = value;
Invalidate();
}
}
[Description("Color scheme to be used")]
public BackColorSchemeType BackColorScheme
{
get{ return _bkgColorScheme; }
set
{
if( _bkgColorScheme != value )
{
_bkgColorScheme = value;
ChangeColorScheme();
Invalidate();
}
}
}
[Description("Sets the alignment of the text")]
public TextAlignStyle TextAlign
{
get{ return _taStyle; }
set
{
if( _taStyle != value )
{
_taStyle = value;
Invalidate();
}
}
}
The BackColor1
and BackColor2
are used to make the background 3D
view. The ShowShadow
makes the text drop a shadow on the background.
The BackColorScheme
is used to select one of the pre-defined color
scheme and TextAlign
is used to align the text on the control.
The control implementation uses two helper functions as
private void ChangeColorScheme()
{
switch(BackColorScheme)
{
case BackColorSchemeType.Office2003Blue:
BackColor = Color.FromArgb(159, 191, 236);
BackColor1 = Color.FromArgb(159, 191, 236);
BackColor2 = Color.FromArgb(54, 102, 187);
break;
case BackColorSchemeType.Office2003Orange:
BackColor = Color.FromArgb(251, 230, 148);
BackColor1 = Color.FromArgb(251, 230, 148);
BackColor2 = Color.FromArgb(239, 150, 21);
break;
case BackColorSchemeType.DarkBlueTinge:
BackColor = Color.FromArgb(89, 135, 214);
BackColor1 = Color.FromArgb(89, 135, 214);
BackColor2 = Color.FromArgb(4, 57, 148);
break;
case BackColorSchemeType.OSXLightGray:
BackColor = Color.FromArgb(242, 242, 242);
BackColor1 = Color.FromArgb(242, 242, 242);
BackColor2 = Color.FromArgb(200, 200, 200);
break;
case BackColorSchemeType.LightGreenVikings:
BackColor = Color.FromArgb(235, 245, 214);
BackColor1 = Color.FromArgb(235, 245, 214);
BackColor2 = Color.FromArgb(195, 224, 133);
break;
case BackColorSchemeType.Office2003Silver:
BackColor = Color.FromArgb(225, 226, 236);
BackColor1 = Color.FromArgb(225, 226, 236);
BackColor2 = Color.FromArgb(150, 148, 178);
break;
case BackColorSchemeType.Office2003Green:
BackColor = Color.FromArgb(234, 240, 207);
BackColor1 = Color.FromArgb(234, 240, 207);
BackColor2 = Color.FromArgb(178, 193, 140);
break;
case BackColorSchemeType.Azure:
BackColor = Color.FromArgb(222, 218, 202);
BackColor1 = Color.FromArgb(222, 218, 202);
BackColor2 = Color.FromArgb(192, 185, 154);
break;
}
}
private PointF CalculateTextLocation()
{
float x = 0.0f, y = 0.0f;
switch(TextAlign)
{
case TextAlignStyle.TAlignLeftTop:
x = 2;
y = 0;
break;
case TextAlignStyle.TAlignLeftBottom:
x = 2;
y = Height - 15;
break;
case TextAlignStyle.TAlignLeftMiddle:
x = 2;
y = (Height - 15) / 2;
break;
case TextAlignStyle.TAlignMiddleTop:
x = (Width - 80) / 2;
y = 0;
break;
case TextAlignStyle.TAlignMiddleMiddle:
x = (Width - 80) / 2;
y = (Height - 15) / 2;
break;
case TextAlignStyle.TAlignMiddleBottom:
x = (Width - 80) / 2;
y = (Height - 15);
break;
default:
throw new InvalidEnumArgumentException("
Invalid Argument for Text Alignment");
}
return new PointF(x, y);
}
The ChangeColorScheme()
defined above just
changes the BackColor properties according to the scheme-type
selected by the client and CalculateTextLocation()
finds the (x,y) location where the text is to be drawn.
Now comes the real drawing part in the OnPaint()
method which contains the following code
base.OnPaint (e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush lgb = new LinearGradientBrush(
new Rectangle(0, 0, Width, Height), BackColor1, BackColor2,
90, false);
e.Graphics.FillRectangle(lgb, 0, 0, Width, Height);
lgb.Dispose();
SolidBrush tb = new SolidBrush(ForeColor);
e.Graphics.MeasureString(Text, Font, Width);
e.Graphics.DrawString(Text,Font,tb,CalculateTextLocation());
tb.Dispose();
if( ShowShadow )
{
SolidBrush tb1 = new SolidBrush(ForeColor);
e.Graphics.MeasureString(Text, Font, Width);
e.Graphics.DrawString(Text, Font, tb1,
CalculateTextLocation().X + 0.5f,
CalculateTextLocation().Y + 0.5f);
tb1.Dispose();
}
Before, we close the discussion about the code, let's see
what are other positions where the control is Invalidated
(i.e. re-painted).
public xpLabel()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
Text = this.Name;
BackColor = Color.FromArgb(159, 191, 236);
ChangeColorScheme();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);
Invalidate();
}
For more updated download, you can visit my website [http://amitbhandari.co.nr].
Known Issues
Since I'm very new to control/component writing so this
control may contain some bugs. So, any comments, criticisms,
bugs, issues...are all welcome.
One known issue which I have found with the control is that
whenever you change ColorScheme
in the properties window, the
BackColor
, BackColor1
and BackColor2
doesn't get updated immediately.
I'm working on it and hope to resolve it very soon!!!
History
v1.0.2594.34043 First initial release.
I: working as software engineer.
My Blog: http://blog.abstractcomputing.in