Enhanced Label control (WinForms)

Ok, after Darren Neimke told me I should start blogging again - I thought for a while what I should blog about; what knowledge do I have that I should share with you guys? I decided that graphical programming is something I have learned to master over the years, so I decided I should start posting about GDI+ and DirectX. I am hoping that I can show off some tricks off the trade and perhaps help people who are new to GDI(+) and/or DirectX with some small snippets of code every now and then... Here's the first little snippet.

Ever wanted to enhance the standard, boring WinForms controls? The Control below shows you how simple it is to make a more neat looking Label control - Enjoy! 


using System;
using
System.ComponentModel;
using
System.Drawing;
using
System.Drawing.Drawing2D;
using
System.Windows.Forms;

namespace
CustomControls
{
    public class
CustomLabel : Label
    {
        private
Color _backColor1;
        private
Color _backColor2;

        [ToolboxItem( true ), Category( "Appearance" ), DefaultValue( typeof
( Color ), "AliceBlue" )]
        public
Color BackColor1
        {
            get { return
_backColor1; }
            set { _backColor1 = value
; }
        }

        [ToolboxItem( true ), Category( "Appearance" ), DefaultValue( typeof
( Color ), "DodgerBlue" )]
        public
Color BackColor2
        {
            get { return
_backColor2; }
            set { _backColor2 = value
; }
        }

        public
CustomLabel()
        {
            BackColor1 = Color.AliceBlue;
            BackColor2 = Color.DodgerBlue;
        }

        protected override void
OnPaintBackground( PaintEventArgs pe )
        {
            Graphics g = pe.Graphics;
            Rectangle rect = pe.ClipRectangle;
            LinearGradientBrush gradientBrush = new
LinearGradientBrush( rect, BackColor1, BackColor2, 70 );

            g.Clear( Parent.BackColor );
            g.FillRectangle( gradientBrush, rect );
        }

        protected override void
OnResize( EventArgs e )
        {
            Refresh();
            base
.OnResize( e );
        }
    }
}

Published Sunday, July 27, 2003 3:09 PM by Aylar
Filed under: , ,

Comments

# re: Enhanced Label control (WinForms)

Friday, August 01, 2003 4:53 AM by Alex Hoffman
Excellent Thomas - great to see some code.

# re: Enhanced Label control (WinForms)

Friday, November 28, 2003 2:04 AM by Branko Hajdenkumer
Nice.

# re: Enhanced Label control (WinForms)

Wednesday, March 10, 2004 6:50 AM by Johann de Swardt
Nice looking control... Very useful for a whole bunch of stuff.

Leave a Comment

(required) 
(required) 
(optional)
(required)