none
Drawing circle around the mouse clicked area on windows form

    Question

  • Hey, I want to draw just a circle around the mouse click. Where the mouse clicks it should be the center of the circle and circle should be drawn around that area. I m new tu system. drawing api. Please help
    • Moved by CoolDadTxMVP Monday, September 30, 2013 2:33 PM Winforms related
    Saturday, September 28, 2013 7:31 AM

Answers

  • You can use the MouseDown event of the form. This will give you the x and y of the mouse click in the MouseEventArg object. In this event, you create a Graphics object and a Rectangle. The Rectangle determines the height and width of the circle. I've created a DrawCircle method for you below. This uses the DrawEllipse method of the Grapics object. The last two parameters are the height and width of the circle. You need to experiment yourself to get the colour and size of the circle you want. You would probably want to hide it again when the user clicks next time.

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        int x = e.X;
        int y = e.Y;
    
        Graphics g;
        g = this.CreateGraphics();
    
        Rectangle rectangle = new Rectangle();
        PaintEventArgs arg = new PaintEventArgs(g, rectangle);
    
        DrawCircle(arg,x,y,100,100);
    }
    
    private void DrawCircle(PaintEventArgs e, int x, int y, int width, int height)
    {
        Pen pen = new Pen(Color.Red, 3);
        e.Graphics.DrawEllipse(pen, x-width/2, y-height/2, width, height);
    }

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.




    Saturday, September 28, 2013 10:41 AM

All replies

  • You can use the MouseDown event of the form. This will give you the x and y of the mouse click in the MouseEventArg object. In this event, you create a Graphics object and a Rectangle. The Rectangle determines the height and width of the circle. I've created a DrawCircle method for you below. This uses the DrawEllipse method of the Grapics object. The last two parameters are the height and width of the circle. You need to experiment yourself to get the colour and size of the circle you want. You would probably want to hide it again when the user clicks next time.

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        int x = e.X;
        int y = e.Y;
    
        Graphics g;
        g = this.CreateGraphics();
    
        Rectangle rectangle = new Rectangle();
        PaintEventArgs arg = new PaintEventArgs(g, rectangle);
    
        DrawCircle(arg,x,y,100,100);
    }
    
    private void DrawCircle(PaintEventArgs e, int x, int y, int width, int height)
    {
        Pen pen = new Pen(Color.Red, 3);
        e.Graphics.DrawEllipse(pen, x-width/2, y-height/2, width, height);
    }

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.




    Saturday, September 28, 2013 10:41 AM
  • Please let us know how this turned out.
    Monday, September 30, 2013 11:14 AM