Hi Guys,
I have seen in MS forums lot of developers asking how to make transparent label. Actually it is pretty simple, when we use the Graphics object in .NET CF.
Here is the complete code for create transparent label. private void DrawLabel (Label label, Graphics gfx)
{
if (label.TextAlign == ContentAlignment.TopLeft)
{
gfx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Bounds);
}
else if (label.TextAlign == ContentAlignment.TopCenter)
{
SizeF size = gfx.MeasureString(label.Text, label.Font);
float left = ((float) this.Width + label.Left) / 2 - size.Width / 2;
RectangleF rect = new RectangleF(left, (float) label.Top, size.Width, label.Height);
gfx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), rect);
}
else //is aligned at TopRight
{
SizeF size = gfx.MeasureString(label.Text, label.Font);
float left = (float) label.Width - size.Width + label.Left;
RectangleF rect = new RectangleF(left, (float) label.Top, size.Width, label.Height);
gfx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), rect);
}
}
I hope this is helpful. |