Gradient button


Gradient button c#

add this class in your form.cs

Download Code

public class rakeshButton : System.Windows.Forms.Button
    {
        // declare two color for linear gradient
        private Color cLeft;
        private Color cRight;

        // property of begin color in linear gradient
        public Color BeginColor
        {
            get
            {
                return cLeft;
            }
            set
            {
                cLeft = value;
            }
        }
        // property of end color in linear gradient
        public Color EndColor
        {
            get
            {
                return cRight;
            }
            set
            {
                cRight = value;
            }
        }

        public rakeshButton()
        {
            // Default get system color
            cLeft = SystemColors.ActiveCaption;
            cRight = SystemColors.Control;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            // declare linear gradient brush for fill background of Button
            LinearGradientBrush GBrush = new LinearGradientBrush(
                new Point(0, 0),
                new Point(this.Width, 0), cLeft, cRight);
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            // Fill with gradient
            e.Graphics.FillRectangle(GBrush, rect);

            // draw text on label
            SolidBrush drawBrush = new SolidBrush(this.ForeColor);
            StringFormat sf = new StringFormat();
            // align with center
            sf.Alignment = StringAlignment.Center;
            // set rectangle bound text
            RectangleF rectF = new RectangleF(0, this.Height / 2 - Font.Height / 2, this.Width, this.Height);
            // output string
            e.Graphics.DrawString(this.Text, this.Font, drawBrush, rectF, sf);

        }
    }

and declare it

 public partial class Form1 : Form
    {
        private rakeshButton mybutton;


-----------and call this object with class insistilization


 public Form1()
        {
            InitializeComponent();
               

            this.rkbutton = new rakeshButton();
            this.rkbutton.BeginColor = System.Drawing.SystemColors.ActiveCaption;
            this.rkbutton.EndColor = System.Drawing.SystemColors.Control;
            this.rkbutton.Name = "gLabel1";
            this.rkbutton.Font = new Font("Verdana", 12F, FontStyle.Bold);
            this.rkbutton.Size = new System.Drawing.Size(352, 56);
            this.rkbutton.TabIndex = 0;
            this.rkbutton.Text = "Test gradient Button";
            this.Controls.Add(rkbutton);
        }





Comments