S
ShakeDoctor
Hi, I have a custom label control which is clickable.
However, the background of the label is white, and you can't set it to
transparent in the property window because it says it is not supported.
Here is the code, what do i need to add to make the BackColor transparent?
public class CustomLabel : Control
{
public ContentAlignment TextAlignment = ContentAlignment.TopLeft;
public CustomLabel()
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground (e);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
SolidBrush brush = new SolidBrush(this.ForeColor);
SizeF s;
s = graphics.MeasureString(this.Text, this.Font);
switch (this.TextAlignment)
{
case ContentAlignment.TopCenter:
graphics.DrawString(this.Text, this.Font, brush, (this.Width - s.Width)
/ 2,(this.Height - s.Height) / 2);
break;
case ContentAlignment.TopLeft:
graphics.DrawString(this.Text, this.Font, brush, 2, (this.Height -
s.Height)/ 2);
break;
case ContentAlignment.TopRight:
graphics.DrawString(this.Text, this.Font, brush, this.Width - s.Width -
2,(this.Height - s.Height) / 2);
break;
}
brush.Dispose();
base.OnPaint (e);
}
public override string Text
{
get {return base.Text;}
set
{
base.Text = value;
//Force to repaint
this.Invalidate();
}
}
However, the background of the label is white, and you can't set it to
transparent in the property window because it says it is not supported.
Here is the code, what do i need to add to make the BackColor transparent?
public class CustomLabel : Control
{
public ContentAlignment TextAlignment = ContentAlignment.TopLeft;
public CustomLabel()
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground (e);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
SolidBrush brush = new SolidBrush(this.ForeColor);
SizeF s;
s = graphics.MeasureString(this.Text, this.Font);
switch (this.TextAlignment)
{
case ContentAlignment.TopCenter:
graphics.DrawString(this.Text, this.Font, brush, (this.Width - s.Width)
/ 2,(this.Height - s.Height) / 2);
break;
case ContentAlignment.TopLeft:
graphics.DrawString(this.Text, this.Font, brush, 2, (this.Height -
s.Height)/ 2);
break;
case ContentAlignment.TopRight:
graphics.DrawString(this.Text, this.Font, brush, this.Width - s.Width -
2,(this.Height - s.Height) / 2);
break;
}
brush.Dispose();
base.OnPaint (e);
}
public override string Text
{
get {return base.Text;}
set
{
base.Text = value;
//Force to repaint
this.Invalidate();
}
}