Gradient Wrap


   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

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

namespace GradientWrap_c
{
    /// <summary>
    /// Summary description for GradientWrap.
    /// </summary>
    public class GradientWrap : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GradientWrap()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      // 
      // GradientWrap
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "GradientWrap";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "GradientWrap";
      this.Load += new System.EventHandler(this.GradientWrap_Load);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new GradientWrap());
        }

    private void GradientWrap_Load(object sender, System.EventArgs e)
    {
    
    }

    protected override void OnPaint( PaintEventArgs e )
    {
      GraphicsPath Path = new GraphicsPath();
      Rectangle R = new Rectangle(10, 10, 50, 50);
      e.Graphics.DrawRectangle(Pens.Black,R);
      Path.AddRectangle(R);
      
//      PathGradientBrush B = new PathGradientBrush(Path.PathPoints); 
      PathGradientBrush B = new PathGradientBrush(Path.PathPoints, 
        WrapMode.Tile);
      Color[] c = { Color.Blue, Color.Aqua, Color.Red };

      B.CenterColor = Color.White;
      B.SurroundColors = c;

      //Small circle inside gradient path
      e.Graphics.FillEllipse(B, 15, 15, 30, 30);
      //Large circle outside gradient path
      e.Graphics.FillEllipse(B, 50, 50, 150, 150);
    }

    }
}


           
          


Gradient Demo



   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GradientBlend_c
{
  /// <summary>
    /// Summary description for GradientBlend.
    /// </summary>
    public class GradientBlend : System.Windows.Forms.Form
    {
    private System.Windows.Forms.HScrollBar BlendWidth;
        private System.ComponentModel.Container components = null;
    private System.Windows.Forms.HScrollBar Skew;
    private System.Windows.Forms.Button cmdDoubleBuffer;

    private int BlWidth;
    private int SkewVal;
    private Rectangle EL1Rect;
    private Rectangle EL2Rect;
    private Region EL1Region;
    private Region EL2Region;
    private Region EL3Region;

        public GradientBlend()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

      //Set up rectangles to draw ellipses in
      EL1Rect = new Rectangle(10, 10, 150, 50);
      EL2Rect = EL1Rect;
      //I could make a new rectangle but I can offset without knowing 
      //anything about the previous rectangle.
      EL2Rect.Offset(200, 0);

      //Set up Regions for invalidation
      EL1Region = new Region(EL1Rect);
      EL2Region = new Region(EL2Rect);
      EL3Region = new Region( new Rectangle(new Point(0, 65), 
                                            new Size(this.Width, 50)));


      //Set up the blend scroll bar
      BlendWidth.Top = 120;
      BlendWidth.Left = this.Width/3;
      BlendWidth.Width = this.Width/3;
      BlendWidth.Minimum = 10;
      BlendWidth.Maximum = 200;
      BlendWidth.SmallChange = 1;
      BlendWidth.LargeChange = 10;
      BlendWidth.Value = BlendWidth.Minimum;

      //Set up the Skew Scroll Bar
      Skew.Top = 145;
      Skew.Left = this.Width/3;
      Skew.Width = this.Width/3;
      Skew.Minimum = 10;
      Skew.Maximum = 40;
      Skew.SmallChange = 1;
      Skew.LargeChange = 10;
      Skew.Value = Skew.Minimum;

      //Set up the double buffer button
      cmdDoubleBuffer.Top = Skew.Top + Skew.Height + 5;
      cmdDoubleBuffer.Width = Skew.Width;
      cmdDoubleBuffer.Left = Skew.Left;
      cmdDoubleBuffer.Text = "Allow Flicker";

      BlWidth = BlendWidth.Value;
      SkewVal = Skew.Value;

      // Set up for double buffering.
      //This, along with invalidating only those areas that need it TOTALY
      //eliminate flicker in this program
      this.SetStyle ( ControlStyles.AllPaintingInWmPaint, true);
      this.SetStyle ( ControlStyles.DoubleBuffer, true);
      this.SetStyle ( ControlStyles.UserPaint, true);

    }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
        //Dispose of our own objects
        EL1Region.Dispose();
        EL2Region.Dispose();
        EL3Region.Dispose();
      }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.BlendWidth = new System.Windows.Forms.HScrollBar();
      this.Skew = new System.Windows.Forms.HScrollBar();
      this.cmdDoubleBuffer = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // BlendWidth
      // 
      this.BlendWidth.Location = new System.Drawing.Point(32, 224);
      this.BlendWidth.Name = "BlendWidth";
      this.BlendWidth.Size = new System.Drawing.Size(192, 16);
      this.BlendWidth.TabIndex = 0;
      this.BlendWidth.Scroll += new System.Windows.Forms.ScrollEventHandler(this.BlendChange);
      // 
      // Skew
      // 
      this.Skew.Location = new System.Drawing.Point(192, 272);
      this.Skew.Name = "Skew";
      this.Skew.Size = new System.Drawing.Size(104, 16);
      this.Skew.TabIndex = 1;
      this.Skew.Scroll += new System.Windows.Forms.ScrollEventHandler(this.SkewColor);
      // 
      // cmdDoubleBuffer
      // 
      this.cmdDoubleBuffer.Location = new System.Drawing.Point(40, 304);
      this.cmdDoubleBuffer.Name = "cmdDoubleBuffer";
      this.cmdDoubleBuffer.Size = new System.Drawing.Size(248, 24);
      this.cmdDoubleBuffer.TabIndex = 2;
      this.cmdDoubleBuffer.Text = "button1";
      this.cmdDoubleBuffer.Click += new System.EventHandler(this.cmdDoubleBuffer_Click);
      // 
      // GradientBlend
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(392, 373);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.cmdDoubleBuffer,
                                                                  this.Skew,
                                                                  this.BlendWidth});
      this.Name = "GradientBlend";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "GradientBlend";
      this.Load += new System.EventHandler(this.GradientBlend_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new GradientBlend());
        }

    private void GradientBlend_Load(object sender, System.EventArgs e)
    {
    }

    protected override void OnPaint ( PaintEventArgs e )
    {
      e.Graphics.SmoothingMode=SmoothingMode.AntiAlias;

      StandardGradient( e.Graphics );
      e.Graphics.DrawLine(Pens.Black, 0, cmdDoubleBuffer.Bottom+10, this.Width,
                          cmdDoubleBuffer.Bottom+10);
      InterpolateGradient( e.Graphics );

      base.OnPaint(e);
    }

    private void StandardGradient( Graphics G )
    {
      //This brush defines how the color is distributed across the whole 
      //graphics container. Any filled object that gets drawn in the container
      //will pick up the color starting with the color gradient at that 
      //particular point on the screen.
      
      LinearGradientBrush B = new LinearGradientBrush(new PointF(0, 20),
                                           new PointF(BlWidth, SkewVal),
                                           Color.Blue,
                                           Color.Red);

      //Draw an image inside the second rectangle
      G.DrawImage(Image.FromFile("Colorbars.jpg"), EL2Rect);
      
      //Draw a line across the screen with the brush
      //to show the repeating pattern
      Pen P = new Pen(B, 15);
      G.DrawLine ( P, 0, 75, this.Width, 75 );
      //Draw a filled ellipse to show how the colors are used
      G.FillEllipse(B, EL1Rect);

      //Change the starting and ending colors
      //Set the alpha so the image below shows through
      Color[] c = {Color.FromArgb(100, Color.LightBlue),
                   Color.FromArgb(100, Color.DarkBlue)};
      B.LinearColors = c;
      P.Brush = B;
      G.DrawLine ( P, 0, 100, this.Width, 100 );
      G.FillEllipse(B, EL2Rect );

      //Reclaim some memory
      c = null;
      P.Dispose();
      B.Dispose();
    }

    private void InterpolateGradient ( Graphics G )
    {
      //Make a set of colors to use in the blend
      Color[] EndColors = {Color.Green,
                           Color.Yellow,
                           Color.Yellow,
                           Color.Blue,
                           Color.Red,
                           Color.Red};

      //These are the positions of the colors along the Gradient line
      float[] ColorPositions = {0.0f, .20f, .40f, .60f, .80f, 1.0f};

      //Fill the blend object with the colors and their positions
      ColorBlend C_Blend = new ColorBlend();
      C_Blend.Colors = EndColors;
      C_Blend.Positions = ColorPositions;
      
      //Make the linear brush and assign the custom blend to it
      LinearGradientBrush B = new LinearGradientBrush ( new Point(10, 110), 
                                                        new Point(140, 110), 
                                                        Color.White, 
                                                        Color.Black );
      B.InterpolationColors = C_Blend;

      //Make a graphics path that we can fill and show custom blended fill
      GraphicsPath Pth = new GraphicsPath();
      Pth.AddEllipse(20, 210, 120, 50);
      Pth.AddString("Filled String", new FontFamily("Impact"), 
                    (int)FontStyle.Italic, 30, new Point(200, 220), 
                    StringFormat.GenericDefault );
      G.FillPath(B, Pth);

      Pen P = new Pen(B, 20);
      G.DrawLine ( P, 0, 300, this.Width, 300 );

      if (P != null)
        P.Dispose();
      if (B != null)
        B.Dispose();
      if (Pth != null)
        Pth.Dispose();
    }
    private void BlendChange(object sender, 
                             System.Windows.Forms.ScrollEventArgs e)
    {
      BlWidth = BlendWidth.Value;
      //Redraw the first ellipse
      this.Invalidate(EL1Region);
      //Redraw the second ellipse
      this.Invalidate(EL2Region);
      //Redraw the lines
      this.Invalidate(EL3Region);
    }

    private void SkewColor(object sender, 
                           System.Windows.Forms.ScrollEventArgs e)
    {
      SkewVal = Skew.Value;
      //Redraw the first ellipse
      this.Invalidate(EL1Region);
      //Redraw the second ellipse
      this.Invalidate(EL2Region);
      //Redraw the lines
      Invalidate(EL3Region);
    }

    private void cmdDoubleBuffer_Click(object sender, System.EventArgs e)
    {
      if (  this.GetStyle( ControlStyles.AllPaintingInWmPaint ) &amp;&amp; 
        this.GetStyle( ControlStyles.DoubleBuffer ) &amp;&amp;
        this.GetStyle( ControlStyles.UserPaint ) )
      {
        cmdDoubleBuffer.Text = "Eliminate Flicker";
        this.SetStyle ( ControlStyles.AllPaintingInWmPaint, false);
        this.SetStyle ( ControlStyles.DoubleBuffer, false);
      }
      else
      {
        cmdDoubleBuffer.Text = "Allow Flicker";
        this.SetStyle ( ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle ( ControlStyles.DoubleBuffer, true);
      }
    }
    }
}


           
          


GradientBlend-c.zip( 7 k)

Gradient Brushes


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GDI_Basics
{
    /// <summary>
    /// Summary description for GradientBrushes.
    /// </summary>
    public class GradientBrushes : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GradientBrushes()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // GradientBrushes
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(388, 298);
            this.Name = "GradientBrushes";
            this.Text = "GradientBrushes";
            this.Resize += new System.EventHandler(this.GradientBrushes_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.GradientBrushes_Paint);

        }
        #endregion



        [STAThread]
        static void Main() 
        {
            Application.Run(new GradientBrushes());
        }

        private void GradientBrushes_Resize(object sender, System.EventArgs e)
        {
            this.Invalidate();
        }

        private void GradientBrushes_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            ArrayList stylesShown = new ArrayList();
            LinearGradientBrush myBrush;
            int y = 20;
            int x = 20;
    
            foreach (LinearGradientMode gradientStyle in System.Enum.GetValues(typeof(LinearGradientMode)))
            {
                myBrush = new LinearGradientBrush(new Rectangle(x, y, 100, 60), Color.Violet, Color.White, gradientStyle);
                e.Graphics.FillRectangle(myBrush, x, y, 100, 60);
                e.Graphics.DrawString(gradientStyle.ToString(), new Font("Tahoma", 8), Brushes.Black, 110 + x, y + 20);
                y += 70;
            }

        }
    }
}



           
          


Gradient Button


   


/*
Code revised from chapter 5


GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward 

Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604

Sample chapter
http://international.us.server12.fileserver.kutayzorlu.com/files/download/2017/01/1604_CustomControls_SampleChapter_uuid-9204cb98-8c2a-4f19-b5c3-3d6d82b4d0cf_crc-0.pdf



For More info on GDI+ Custom Control with Microsoft Visual C# book 
visit website www.packtpub.com 


*/ 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.gradientButton1 = new GradientButton();
            this.SuspendLayout();
            // 
            // gradientButton1
            // 
            this.gradientButton1.BackColor = System.Drawing.SystemColors.ControlDark;
            this.gradientButton1.BorderStyle = System.Windows.Forms.Border3DStyle.Raised;
            this.gradientButton1.EndColor = System.Drawing.SystemColors.ActiveCaption;
            this.gradientButton1.Location = new System.Drawing.Point(91, 53);
            this.gradientButton1.Name = "gradientButton1";
            this.gradientButton1.Size = new System.Drawing.Size(100, 40);
            this.gradientButton1.StartColor = System.Drawing.SystemColors.ControlLightLight;
            this.gradientButton1.TabIndex = 0;
            this.gradientButton1.Text = "Gradient Button";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.gradientButton1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private GradientButton gradientButton1;

        private void controlPart2_Click(object sender, EventArgs e)
        {

        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        
    }

    public partial class GradientButton : BorderGradientPanel
    {
        public GradientButton()
        {
            UpdateAppearance();
            InitializeComponent();
        }
        private bool clicked = false;
        private void UpdateAppearance()
        {
            if (clicked)
            {
                StartColor = SystemColors.Control;
                EndColor = SystemColors.ControlLight;
                BorderStyle = Border3DStyle.Sunken;
            }
            else
            {
                StartColor = SystemColors.ControlLight;
                EndColor = SystemColors.Control;
                BorderStyle = Border3DStyle.Raised;
            }

        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                clicked = true;
                UpdateAppearance();
            }
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                clicked = false;
                UpdateAppearance();
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Brush foreBrush = new SolidBrush(ForeColor);
            SizeF size = pe.Graphics.MeasureString(Text, Font);
            PointF pt = new PointF((Width - size.Width) / 2, (Height - size.Height) / 2);
            if (clicked)
            {
                pt.X += 2;
                pt.Y += 2;
            }
            pe.Graphics.DrawString(Text, Font, foreBrush, pt);
            foreBrush.Dispose();
        }
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
        
    }


    public partial class BorderGradientPanel : Control
    {
        private Border3DStyle borderStyle = Border3DStyle.Sunken;
        private Color startColor = SystemColors.Control;
        private Color endColor = SystemColors.Control;
        public Color EndColor
        {
            get
            {
                return endColor;
            }
            set
            {
                if (endColor != value)
                {
                    endColor = value;
                    Invalidate();
                }
            }
        }
        public Color StartColor
        {
            get
            {
                return startColor;
            }
            set
            {
                if (startColor != value)
                {
                    startColor = value;
                    Invalidate();
                }
            }
            
        }
        public Border3DStyle BorderStyle
        {
            get
            {
                return borderStyle;
            }
            set
            {
                if (borderStyle != value)
                {
                    borderStyle = value;
                    Invalidate();
                }
            }
        }
        public BorderGradientPanel()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(0, Height), startColor, endColor);
            e.Graphics.FillRectangle(brush, ClientRectangle);
            ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, borderStyle);
            brush.Dispose();
        }

        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }




    public class ScrollArrowButton : ControlPart
    {
        protected override void RenderControl(Graphics g, ButtonState buttonState, CheckState checkState)
        {
            //ControlPaint.DrawScrollButton(g, ClientRectangle, ScrollButton.Up,buttonState);
            ControlPaint.DrawScrollButton(g, ClientRectangle, sButton, buttonState);
        }
        private ScrollButton sButton = ScrollButton.Up;
        public ScrollButton ButtonType
        {
            get
            {
                return sButton;
            }
            set
            {
                if (sButton != value)
                {
                    sButton = value;
                    Invalidate();
                }
            }
        }
    }

    public class CheckButton : ControlPart
    {
        protected override void RenderControl(Graphics g, ButtonState buttonState, CheckState checkState)
        {
            ButtonState bstate = buttonState;
            switch (checkState)
            {
                case CheckState.Checked: bstate = ButtonState.Checked; break;
                case CheckState.Indeterminate: bstate = ButtonState.All; break;
            }
            ControlPaint.DrawCheckBox(g, ClientRectangle, bstate);
        }

    }

    public class ControlPart : Control
    {
        private ButtonState buttonState = ButtonState.Flat;
        private CheckState checkState = CheckState.Unchecked;
        //indicates wheter the mouse is hovering over the control
        protected bool mouseOver = false;
        public ControlPart()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            buttonState = ButtonState.Normal;
            mouseOver = true;
            Invalidate(true);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            buttonState = ButtonState.Flat;
            mouseOver = false;
            Invalidate(true);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.Focus();
            if (!(e.Button == MouseButtons.Left)) return;
            buttonState = ButtonState.Pushed;
            switch (checkState)
            {
                case CheckState.Checked: checkState = CheckState.Unchecked; break;
                case CheckState.Unchecked: checkState = CheckState.Checked; break;
                case CheckState.Indeterminate: checkState = CheckState.Unchecked; break;
            }
            Invalidate(true);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (!((e.Button &amp; MouseButtons.Left) == MouseButtons.Left)) return;
            buttonState = ButtonState.Normal;
            Invalidate(true);
        }
        protected virtual void RenderControl(Graphics g, ButtonState buttonState, CheckState checkState)
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            RenderControl(e.Graphics, buttonState, checkState);
        }

    }


           
          


Gradient Label


   


/*
Code revised from chapter 3


GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward 

Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604

Sample chapter
http://international.us.server12.fileserver.kutayzorlu.com/files/download/2017/01/1604_CustomControls_SampleChapter_uuid-6297319b-9374-4099-b18c-41b668f47669_crc-0.pdf


For More info on GDI+ Custom Control with Microsoft Visual C# book 
visit website www.packtpub.com 


*/ 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GradientLabelTest00
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.gradientLabel1 = new GradientLabelTest00.GradientLabel();
            this.SuspendLayout();

            this.gradientLabel1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.gradientLabel1.BackColor2 = System.Drawing.SystemColors.ControlDarkDark;
            this.gradientLabel1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.gradientLabel1.Location = new System.Drawing.Point(66, 68);
            this.gradientLabel1.Name = "gradientLabel1";
            this.gradientLabel1.Size = new System.Drawing.Size(150, 47);
            this.gradientLabel1.TabIndex = 0;
            this.gradientLabel1.Text = "Gradient Label";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.gradientLabel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private GradientLabel gradientLabel1;        
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

    }
    public partial class GradientLabel : Control
    {
        private Color backColor2 = SystemColors.ControlLight;
        public GradientLabel()
        {
            InitializeComponent();
        }
        public Color BackColor2
        {
            get
            {
                return backColor2;
            }
            set
            {
                if (backColor2 != value)
                {
                    backColor2 = value;
                    Invalidate();
                }
            }
        }

        private void GradientLabel_Paint(object sender, PaintEventArgs e)
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {
                base.OnPaint(e);
                LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(0, Height), BackColor, BackColor2);
                e.Graphics.FillRectangle(brush, ClientRectangle);
                Brush foreBrush = new SolidBrush(ForeColor);
                SizeF textSize = e.Graphics.MeasureString(Text, Font);
                e.Graphics.DrawString(Text, Font, foreBrush, (Width - textSize.Width) / 2, (Height - textSize.Height) / 2);
                brush.Dispose();
                foreBrush.Dispose();
        }

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Component Designer generated code

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.ForeColor = System.Drawing.SystemColors.HotTrack;
            this.Size = new System.Drawing.Size(150, 24);
            this.Text = "Gradient Label";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.GradientLabel_Paint);
            this.ResumeLayout(false);
        }
        #endregion        
    }
}
           
          


Use LinearGradientBrush to draw a Rectangle


   

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

public class Form1 : System.Windows.Forms.Form{
  private System.ComponentModel.Container components = null;

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);

         Brush linearGradientBrush = new LinearGradientBrush(
            new Rectangle(10, 60, 50, 50), Color.Blue, Color.White, 45);
         g.FillRectangle(linearGradientBrush, new Rectangle(10, 60, 50, 50));

         linearGradientBrush.Dispose();
         
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Path Gradient Brush from Graphics Path


   



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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      

      Graphics g = e.Graphics;
      GraphicsPath gp = new GraphicsPath();

      gp.AddLine(10, 10, 110, 15);
      gp.AddLine(110, 15, 100, 95);
      gp.AddLine(100, 95, 15, 110);
      gp.CloseFigure();

      g.FillRectangle(Brushes.White, this.ClientRectangle);
      g.SmoothingMode = SmoothingMode.AntiAlias;

      PathGradientBrush pgb = new PathGradientBrush(gp);
      pgb.CenterColor = Color.White;
      pgb.SurroundColors = new Color[] { Color.Red };
      g.FillPath(pgb, gp);

      g.DrawPath(Pens.Black, gp);
      pgb.Dispose();
      gp.Dispose();

    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }