Custom Shaped (Oval) Button

Chapter 7, Lesson 2 of the MCAD/MCSD Self-Paced Training Kit: Developing Windows®-Based Applications with Microsoft® Visual Basic® .NET and Microsoft Visual C#® .NET, Second Ed is about authoring controls. While reading this I decided to create my own oval-shaped control. I was amazed at how simple it is to create controls that are not your run-of-the-mill rectangular-shaped control by simply inheriting from Button and overriding the OnPaint method.
 
 

protected override void OnPaint(PaintEventArgs e) {

    System.Drawing.Rectangle rect =

new Rectangle( new System.Drawing.Point(0, 0),

                new System.Drawing.Size(this.Width, this.Height ));

    System.Drawing.Drawing2D.GraphicsPath myPath = new

    System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);

    myPath.AddEllipse(rect);

    Region myRegion =

new Region(myPath);

    this.Region = myRegion;

    using( System.Drawing.Drawing2D.LinearGradientBrush brush =

            new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.CornflowerBlue, Color.White, 45 ) ) {

            e.Graphics.FillEllipse( brush, 0,0, this.Width, this.Height);

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s