Monday, 10 February 2014

Create button from Control ( customize button ) " Using C# "

Just Save as c# class after making your own changes & and add as user control...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowEvents
{
    enum MouseState {none, down, up, over, leave };

    public class PictureButton : Control
    {
        MouseState mouseState;
        public Color _color { get; set; }
        Color _colorBase { get; set; }
         Color _colorBorder { get; set; }

        SolidBrush _brush { get; set; }
        SolidBrush _brushBase { get; set; }
        SolidBrush _brushBorder { get; set; }

        SolidBrush _brushTmp = new SolidBrush(Color.Black);


        public PictureButton()
        {

            mouseState = MouseState.none;
            _colorBase =  Color.Green;
            _colorBorder = Color.FromArgb(150, _colorBase);
            _color = Color.FromArgb(200, _colorBorder);

            _brushBase = new SolidBrush(_colorBase);
            _brush = new SolidBrush(_color);
            _brushBorder = new SolidBrush(_colorBorder);

            this.DoubleBuffered = true;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            mouseState = MouseState.down;
            Invalidate();
           
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            mouseState = MouseState.up;
            Invalidate();
        }

        protected override void OnMouseHover(EventArgs e)
        {
            mouseState = MouseState.over;
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            mouseState = MouseState.none;
            Invalidate();
        }

        // Override the OnPaint method to draw the background image and the text.
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(ClientRectangle);
            this.Region = new Region(path);

            switch(mouseState)
            {
                case MouseState.none:
                    g.FillRectangle(_brushBorder, ClientRectangle);
                    g.FillRectangle(_brushBase, ClientRectangle.X + 4, ClientRectangle.Y + 4, ClientRectangle.Width - 8, ClientRectangle.Height - 8);
                    break;
                case MouseState.down:
                    using (PathGradientBrush gradient = new PathGradientBrush(path))
                    {
                   //     gradient.CenterPoint = new PointF(ClientSize.Width / 2, ClientSize.Height / 2);
                   //     gradient.CenterColor = Color.Honeydew;
                    //    gradient.SurroundColors = new Color[] { _brushUp.Color };
                        g.FillRectangle(_brushBase, ClientRectangle);
                    }
                    break;
                case MouseState.up:
                    using (PathGradientBrush gradient = new PathGradientBrush(path))
                    {
                        g.FillRectangle(_brushBorder, ClientRectangle);
                        g.FillRectangle(_brushBase, ClientRectangle.X + 4, ClientRectangle.Y + 4, ClientRectangle.Width - 8, ClientRectangle.Height - 8);
                    }
                    break;
                case MouseState.over:
                    using (PathGradientBrush gradient = new PathGradientBrush(path))
                    {
                        g.FillRectangle(_brushBorder, ClientRectangle);
                        g.FillRectangle(_brushBase, ClientRectangle.X + 4, ClientRectangle.Y + 4, ClientRectangle.Width - 8, ClientRectangle.Height - 8);
                    }
                    break;
            }

            // Draw the text if there is any.
            if (this.Text.Length > 0)
            {
                SizeF size = e.Graphics.MeasureString(this.Text, this.Font);

                // Center the text inside the client area of the PictureButton.
                e.Graphics.DrawString(this.Text,
                    this.Font,
                    new SolidBrush(this.ForeColor),
                    (this.ClientSize.Width - size.Width) / 2,
                    (this.ClientSize.Height - size.Height) / 2);
            }

            base.OnPaint(e);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // PictureButton
            //
            this.Text = " = ";
            this.ResumeLayout(false);
        }
    }
}

No comments:

Post a Comment