C# Exercises

Home AgriMetSoft About Contact

Simple Ellipse or Circle Button in WinForms c#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Simple_Ellipse_Button
{
  public partial class Form1 : Form
  {
    int a = 70;//---‌Big Diameter
    int b = 70;//---‌Small Diameter
    int c = 2;
    int h = 0, k = 0;
    public Form1()
    {
      InitializeComponent();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      //-----Border----------
      Brush brush = new SolidBrush(Color.DarkGreen);
      g.FillEllipse(brush, 0, 0, a, b);
      //-----Fill Shape----------
      Brush brush1 = new SolidBrush(Color.LightGreen);
      g.FillEllipse(brush1, c, c, a - c - 1, b - c - 1);
    }
    private void panel1_Click(object sender, EventArgs e)
    {
      var pt = panel1.PointToClient(Cursor.Position);
      int x = pt.X;
      int y = pt.Y;
      if (checkpoint(h, k, x, y, a, b) >= 1)
        MessageBox.Show("Outside");
      else
        MessageBox.Show("Inside");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      panel1.Width = a;
      panel1.Height = b;
      label1.Location = new Point((a - label1.Width) / 2, (b - label1.Height) / 2);
      panel1.Cursor = Cursors.Hand;
      label1.Cursor = Cursors.Hand;
      label1.BackColor = Color.Transparent;
      panel1.Paint += panel1_Paint;
      panel1.Click += panel1_Click;
    }
    static double checkpoint(double h, double k, double x, double y, int a, int b)
    {
      // checking the equation of
      // ellipse with the given point
      double p = ((double)Math.Pow((x - h), 2)
            / (double)Math.Pow(a, 2))
           + ((double)Math.Pow((y - k), 2)
             / (double)Math.Pow(b, 2));
      return p;
    }
  }
}

		
	 


Download the project of Visual Studio 2013 in DropBox Download


C# WinForms Simple Ellipse or Circle Button | Clickable Shapes in C#


List of Exercises