Label with background transparent that is shown above a PictureBox

Last post 05-15-2008 2:13 by mg_miro_86. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 05-13-2008 22:17

    Label with background transparent that is shown above a PictureBox

    Hello,

    I 'd like to know how can I do a label with background transparent that is shown above a PictureBox.

    Thank you!

     

  • 05-13-2008 23:01 In reply to

    Re: Label with background transparent that is shown above a PictureBox

    Hi Lirulis,

    The only way I know to do this is registering a handler for the PictureBox's Paint event, then use the GDI to draw your text on top of the picture when it is painted.  Your handler will look something like this:

            private void imgMap_Paint(object sender, PaintEventArgs e)

    and you'll want to create the appropriate font and brush, then call e.Graphics.DrawString().

    Hope this helps,

    ----Scott.


  • 05-14-2008 2:56 In reply to

    Re: Label with background transparent that is shown above a PictureBox

     Thank you very much!

  • 05-15-2008 2:13 In reply to

    Re: Label with background transparent that is shown above a PictureBox

    Use this class:

    public enum Align { Left, Center };

    public class TransparentLabel : Control

    {

    private Align textAlign = Align.Left;public Align TextAlign

    {

    set

    {

    textAlign =
    value;

    }

    }

    protected override void OnPaintBackground(PaintEventArgs e)

    {

    }

    private bool bPaintOnce = false;protected override void OnPaint(PaintEventArgs e)

    {

    if (!bPaintOnce)

    {

    bPaintOnce = true;

    this.Visible = false;

    this.Parent.Invalidate(this.Bounds);

    this.Parent.Update();

    this.Visible = true;

    return;

    }

    else

    {

    bPaintOnce =
    false;

    Graphics g = e.Graphics;

    Font font = this.Font;

    SolidBrush brush = new SolidBrush(this.ForeColor);

    if (textAlign == Align.Center)

    {

    SizeF textSize = new SizeF();

    textSize = e.Graphics.MeasureString(Text, font);

    float x = 0, y = 0;if (textSize.Width < Width)

    x = (Width - textSize.Width) / 2;

    if (textSize.Height < Height)

    y = (Height - textSize.Height) / 2;

    g.DrawString(
    this.Text, font, brush, x, y);

    }

    else

    {

    g.DrawString(
    this.Text, font, brush, 1, 1);

    }

    g.Dispose();

    }

    }

    public override string Text

    {

    get

    {

    return base.Text;

    }

    set

    {

    if (this.Visible)

    {

    this.Visible = false;

    base.Text = value;

    this.Visible = true;

    }

    else

    {

    base.Text = value;

    }

    }

    }

    }

Page 1 of 1 (4 items)