Create Ellipse Control in C#

Create your own Ellipse Control in Windows Forms Applications for free without using any third party tool or framework. Ellipse tool is used to round the corners of different elements of C# like a Border Less Form, Panel, GridView e.t.c.

In this short example we’ll create an ellipse tool for C# desktop Applications of WinForm Apps.

Tools Required:

  1. Visual Studio 2010 or later version.

Steps to Follow:

  1. Open Visual Studio and Create a new windows forms applications project or you continue with your existing WinForm Application Project.
  2. Go to the Solution Explorer and right click on project file and Add->Class.
  3. Name this class as ElipseControl and inherit it from main Component class.
  4. Add the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ElipseToolDemo
{
    class ElipseControl : Component
    {
        private Control hostControl;
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
            (
               int nLeftRect,
               int nTopRect,
               int nRightRect,
               int nBottomRect,
               int nWidthEllipse,
               int nHeightEllipse
            );
        private Control _cntrl;
        private int _CornerRadius = 30;

        public Control TargetControl
        {
            get { return _cntrl; }
            set
            {
                _cntrl = value;
                _cntrl.SizeChanged += (sender, eventArgs) => _cntrl.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, _cntrl.Width, _cntrl.Height, _CornerRadius, _CornerRadius));
            }
        }

        public int CornerRadius
        {
            get { return _CornerRadius; }
            set
            {
                _CornerRadius = value;
                if(_cntrl != null)
                    _cntrl.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, _cntrl.Width, _cntrl.Height, _CornerRadius, _CornerRadius));
            }
        }
       
       
    }
}

In this control, we have added two main properties which are TargetControl and CornerRadius. From the target Control property, we can select the control on which we want to add the ellipse (round corner) effect. From border Radius Property, we can set the roundness of the borders of the Targeted Control.

Video Tutorial:

Watch full video tutorial to learn how to create ellipse control in C#.

Ellipse Tool

Source Code:

I Hope you enjoyed this article. Please don’t forget to Subscribe our official YouTube Channel C# Ui Academy

4 thoughts on “Create Ellipse Control in C#

  • June 10, 2020 at 11:47 am
    Permalink

    Thanks for another informative web site. Where else could I get that type of information written in such a perfect way? I have a project that I’m just now working on, and I’ve been on the look out for such information.

    Reply
  • June 10, 2020 at 1:06 pm
    Permalink

    I simply couldn’t go away your site before suggesting that I actually loved the standard info a person provide on your visitors? Is going to be back continuously in order to check out new posts

    Reply
  • June 17, 2020 at 12:26 pm
    Permalink

    I couldn’t resist commenting. Very well written!

    Reply
  • June 28, 2020 at 1:31 pm
    Permalink

    Fantastic site. Plenty of useful info here. I抦 sending it to some friends ans also sharing in delicious. And certainly, thanks for your sweat!

    Reply

Leave a Reply

You have to agree to the comment policy.