Tuesday, July 15, 2008

Transparent Label in .NET Compact framework

Hi Guys,

I seen in MS forums lof of guys, asking how to make transparent label.
it's pretty simple, when we use the Graphics object in .NET CF.

Here 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);

}

}


Thank you

1 comment:

giggle of the day said...

This doesn't seem to work.
I put it into a Transparent label class and when I tried to use the transparent label on top of an image but the label is not transparent.