Click here to Skip to main content
13,664,248 members
Click here to Skip to main content
Add your own
alternative version


102 bookmarked
Posted 16 Aug 2007

A Transparent/Translucent Label Control with Moving Caption

, 16 Aug 2007
Rate this:
Please Sign up or sign in to vote.
This article describes the creation of a Label custom control that can be translucent to the background.


This article describes the creation of a Label custom control that can be translucent to the background.

The Transparent Label Source Code

Screenshot - Lotus.jpg

The Transparent Label has twelve added properties

  • BackColor - Gets or sets the background color of the control.
  • Opacity - The alpha value used to blend the control's background. Valid values are 0 through 255.
  • Radius - Radius of rounded borders.
  • ShapeBorderStyle - Style of border to be drawn around the control.
  • BorderColor - The color of border to be drawn around the control.
  • Caption - The text associated with the control.
  • Font - The font used to display text in the control.
  • Forecolor - The foreground color of this component which is used to display the text.
  • TextAlign - Text alignment (Left, Right or Center) only with Moving None.
  • Moving - Text Moving (None, RightToLeft, DownToUp, LeftToRight, UpToDown).
  • MovingActive - Activate the text movement.
  • DimmedColor - Dims the text color when the mouse is passing over the control.

    The OnPaint call has three simple methods, DrawBorder, DrawLabelBackground, DrawText.

    This is the OnPaint method

    protected override void OnPaint(PaintEventArgs e)
        SmoothingMode sm = e.Graphics.SmoothingMode;
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        if (_borderStyle == ShapeBorderStyles.ShapeBSFixedSingle)
        e.Graphics.SmoothingMode = sm;

    This is the DrawBorder method:

    private void DrawBorder(Graphics g)
        Rectangle rect = this.ClientRectangle;
        using (GraphicsPath bp = GetPath(rect, _radius))
            using (Pen p = new Pen(_borderColor))
                g.DrawPath(p, bp);

    This is the DrawLabelBackground method:

    private void DrawLabelBackground(Graphics g)   
        Rectangle rect = this.ClientRectangle;
        iRect = rect;
        rect.Width -= 2;
        rect.Height -= 2;
        using (GraphicsPath bb = GetPath(rect, _radius))
            using (Brush br = new SolidBrush(
                Color.FromArgb(_opacity, _backColor)))
                g.FillPath(br, bb);

    This is the GetPath method, called by DrawBorder and DrawLabelBackground

    protected GraphicsPath GetPath(Rectangle rc, int r)
    //  Build the path with the round corners in the rectangle
    //  r is the radious of rounded corner.
        int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
        r = r << 1;
        GraphicsPath path = new GraphicsPath();
        if (r > 0)
            //  If the radious of rounded corner is greater than one side then
            //  do the side rounded
            if (r > h) { r = h; };                           //Rounded
            if (r > w) { r = w; };                           //Rounded
            path.AddArc(x, y, r, r, 180, 90);                //Upper left corner
            path.AddArc(x + w - r, y, r, r, 270, 90);        //Upper right corner
            path.AddArc(x + w - r, y + h - r, r, r, 0, 90);  //Lower right corner
            path.AddArc(x, y + h - r, r, r, 90, 90);         //Lower left corner
            // If the radious of rounded corner is zero then the path is 
            // a rectangle
        return path;

    This is DrawText method.

    protected void DrawText(PaintEventArgs pe)
        //This is a workaround to get MeasureString to work properly
        //pe.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
        SizeF sz = pe.Graphics.MeasureString(_text, base.Font);
        switch (_moving)
        case MoveType.None:
        case MoveType.RightToLeft:
        case MoveType.DownToUp:
        case MoveType.LeftToRight:
        case MoveType.UpToDown:
        //Rectangle bounds for the text
        txtRect = new Rectangle(this.pointX, this.pointY,
            (int)sz.Width + 1, (int)sz.Height);
        //If the mouse is passing over the text it is selected and will be dimmed
        //otherwise nothing.
        Brush brText = new SolidBrush(base.ForeColor);
        Brush brTextDimmed = new SolidBrush(_dimmedColor);
        if (_isSelected)

    The methods NoMove(), MoveRigntToLeft(), MoveDownToUp(), MoveLeftToRight() and MoveUpToDown()updates, for each MoveType, the coordinates of text rectangle.

    protected void NoMove()
        //Align text
        switch (_textAlign)
        case TextAlignment.Left:
            pointX = (int)this.iRect.X;
        case TextAlignment.Center:
            pointX = (this.iRect.Width - this.txtRect.Width) / 2;
        case TextAlignment.Right:
            pointX = (this.iRect.Width - this.txtRect.Width);
        pointY = (this.iRect.Height - this.txtRect.Height) / 2;
    protected void MoveRightToLeft()
        if (pointX < -this.txtRect.Width)
        { pointX = this.iRect.X + this.iRect.Width; }
        { pointX -= 2; }
        pointY = (this.iRect.Height - this.txtRect.Height) / 2;
    protected void MoveDownToUp()
        pointX = (this.iRect.Width - this.txtRect.Width) / 2;
        if (pointY < -this.txtRect.Height)
        { pointY = (int)this.iRect.Y + this.iRect.Height; }
        { pointY -= 2; }
    protected void MoveLeftToRight()
        if (pointX > this.iRect.X + this.iRect.Width)
        { pointX = this.iRect.X - this.txtRect.Width; }
        { pointX += 2; }
        pointY = (this.iRect.Height - this.txtRect.Height) / 2;
    protected void MoveUpToDown()
        pointX = (this.iRect.Width - this.txtRect.Width) / 2;
        if (pointY > this.iRect.Y + this.iRect.Height)
        { pointY = (int)this.iRect.Y - this.iRect.Height; }
        { pointY += 2; }

    If the mouse is passing over the text it will be dimmed, for that we use events OnMouseEnter and OnMouseLeave.

    protected override void OnMouseEnter(EventArgs e)
        _isSelected = true;
    protected override void OnMouseLeave(EventArgs e)
        _isSelected = false;

    This component uses a timer to update and invalidate the Draw event OnPaint.

    private void timer1_Tick(object sender, System.EventArgs e)


    I'd like express my gratitude to Andreas for to fix the bug in my article "Two First Controls -A Shape and an Inherited Label Control" and to Jason (Apuhjee) who inspired this new article for your interest in if there was a way to alpha blend the shape with the background.


    12/08/2007 First Version.

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Daniel Ortiz Acuna
    Web Developer
    Argentina Argentina
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    Suggestioni hope it can support display mulitline words Pin
    plaodj23-May-12 6:06
    memberplaodj23-May-12 6:06 
    GeneralMy vote of 5 Pin
    plaodj23-May-12 6:00
    memberplaodj23-May-12 6:00 
    GeneralSmall modification to the Component Class DAOTransparentLabel on Region Metthods to Solve Problem of Text Centerlization Pin
    kshadway27-Nov-09 5:35
    memberkshadway27-Nov-09 5:35 
    GeneralA really nice effort !! Pin
    damon8818-Nov-09 4:44
    memberdamon8818-Nov-09 4:44 
    Generalonly translucent to windows form background image Pin
    jtby211-Sep-09 12:03
    memberjtby211-Sep-09 12:03 
    GeneralThanks for you control Pin
    k^s5-Mar-08 5:37
    memberk^s5-Mar-08 5:37 
    GeneralStrange alignment error Pin
    Johnny J.24-Jan-08 6:09
    memberJohnny J.24-Jan-08 6:09 
    GeneralRe: Strange alignment error Pin
    Johnny J.24-Jan-08 6:11
    memberJohnny J.24-Jan-08 6:11 
    AnswerRe: Strange alignment error Pin
    Morenosoft28-Jan-08 23:25
    memberMorenosoft28-Jan-08 23:25 
    GeneralRe: Strange alignment error Pin
    Johnny J.29-Jan-08 3:08
    memberJohnny J.29-Jan-08 3:08 
    GeneralRe: Strange alignment error Pin
    Daniel Ortiz Acuna30-Jan-08 7:33
    memberDaniel Ortiz Acuna30-Jan-08 7:33 
    GeneralProblems with MovingActive=false Pin
    kiweed28-Nov-07 5:50
    memberkiweed28-Nov-07 5:50 
    GeneralScroll Text Only when it does not fit the Label Pin
    Sukhjinder_K9-Sep-07 4:20
    memberSukhjinder_K9-Sep-07 4:20 
    GeneralAdding an Interval Property to Adjust Delay [modified] Pin
    Sukhjinder_K9-Sep-07 2:16
    memberSukhjinder_K9-Sep-07 2:16 
    QuestionHow do you get to work over an arbitrary form? Pin
    enkur23-Aug-07 19:20
    memberenkur23-Aug-07 19:20 
    QuestionUsing the label as a form Pin
    nagarsoft17-Aug-07 2:44
    membernagarsoft17-Aug-07 2:44 
    AnswerRe: Using the label as a form Pin
    Daniel Ortiz Acuna17-Aug-07 10:09
    memberDaniel Ortiz Acuna17-Aug-07 10:09 
    GeneralA Suggestion Pin
    Georgi Atanasov16-Aug-07 20:23
    memberGeorgi Atanasov16-Aug-07 20:23 
    GeneralLooks very nice + a suggestion Pin
    Liam O'Hagan16-Aug-07 17:38
    memberLiam O'Hagan16-Aug-07 17:38 
    GeneralFelicitaciones Pin
    kresso16-Aug-07 16:15
    memberkresso16-Aug-07 16:15 
    GeneralRe: Felicitaciones Pin
    Daniel Ortiz Acuna17-Aug-07 10:30
    memberDaniel Ortiz Acuna17-Aug-07 10:30 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
    Web01-2016 | 2.8.180810.1 | Last Updated 16 Aug 2007
    Article Copyright 2007 by Daniel Ortiz Acuna
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid