How to detect if mouse has clicked inside of a certain shape in c# on winform app?

OneStig

Let's say that I have a win form app and I have a picture box called pictureBox1. Then I run the following code:

public System.Drawing.Graphics graphics;
public System.Drawing.Pen blackPen = new System.Drawing.Pen(Color.Black, 2);

public void drawVennDiagram()
{
    graphics = pictureBox1.CreateGraphics();
    graphics.DrawEllipse(blackPen, 0, 0, 100, 100);
    graphics.DrawEllipse(blackPen, 55, 0, 100, 100);
}

If I call drawVennDiagram() , it will draw two circles in pictureBox1 and the circles overlap just enough to look like a venn diagram.

What I am trying to achieve is as follows:

  • Run Method A if the mouse clicks anywhere outside of the venn diagram but in the picturebox.

  • Run Method B if the mouse clicks only inside of the first circle.

  • Run Method C if the mouse clicks inside of both circles.

  • Run Method D if the mouse clicks only inside of the second circle.

So far I have written the code below, which essentially tracks where the cursor clicks, but I have no way of figuring out which parameter (a, b, c, d) the cursor location follows.

private void pictureBox1_Click(object sender, EventArgs e)
{
    this.Cursor = new Cursor(Cursor.Current.Handle);
    int xCoordinate = Cursor.Position.X;
    int yCoordinate = Cursor.Position.Y;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    int xCoordinate = e.X;
    int yCoordinate = e.Y;
}

What is the best way to achieve this?

Willem Van Onsem

Yes. Given a circle is positioned with center (xc,yc) and radius r, a coordinate (x,y) is within the circle if: (x-xc)2+(y-yc)2≤r2.

Given we know that, we also know that your circles have a center at (50,50) and (105,50) and each have a radius of 50. So now we define a method:

public static bool InsideCircle (int xc, int yc, int r, int x, int y) {
    int dx = xc-x;
    int dy = yc-y;
    return dx*dx+dy*dy <= r*r;
}

Now you can use:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
    int x = e.X;
    int y = e.Y;
    bool inA = InsideCircle(50,50,50,x,y);
    bool inB = InsideCircle(105,50,50,x,y);
    if(inA && inB) {
        C();
    } else if(inA) {
        B();
    } else if(inB) {
        D();
    } else {
        A();
    }
}

Note however that for now, the two circles you paint do not overlap anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to detect if a link, inside a UITextView has been clicked, Swift

From Dev

How to detect a Winforms app has been idle for certain amount of time

From Dev

How to detect if mouse is clicked other than $(this) element

From Dev

Detect mouse clicked on GUI

From Dev

Detect mouse clicked on GUI

From Dev

How to detect when mouse is outside of a certain circle?

From Dev

How to detect mouse clicked location on an image with custom shaped cursor in c++

From Dev

How to detect when mouse has stopped

From Dev

How to detect if a click has happened inside a box

From Dev

Android: detect if nearby device has certain app installed

From Dev

How to make a turtle object look at where the mouse has clicked

From Dev

How to detect user has clicked Don't Allow access to camera

From Dev

How do I detect which radio button has been clicked?

From Dev

How to detect if the user has clicked on "Quit" in a zenity list

From Dev

How to detect which item in a Matplotlib PathCollection has been clicked on?

From Java

How to detect mouse clicks inside nested buttons in pygame

From Dev

OpenGL- C++ using mouse click to move shape to the clicked position

From Dev

OpenGL- C++ using mouse click to move shape to the clicked position

From Java

Processing: How do you give a shape a value based on the mouse button clicked?

From Dev

How can I bring my shape that I painted with paintComponent and clicked with a mouse to front?

From Dev

Detect if input was touched (tablet) or clicked (mouse)

From Dev

Detect if input was touched (tablet) or clicked (mouse)

From Dev

How can I detect if an user has stopped walking using C# in my windows store app?

From Dev

Windows Phone 8: C# XAML how to detect shape collisions

From Dev

Windows Phone 8: C# XAML how to detect shape collisions

From Dev

How to detect if a button has been clicked OR an input's value has changed using jQuery?

From Dev

How do I draw a rectangle every time the mouse is clicked in a certain area?

From Dev

How do I draw a rectangle every time the mouse is clicked in a certain area?

From Dev

Detect a circular shape inside image in MATLAB

Related Related

  1. 1

    How to detect if a link, inside a UITextView has been clicked, Swift

  2. 2

    How to detect a Winforms app has been idle for certain amount of time

  3. 3

    How to detect if mouse is clicked other than $(this) element

  4. 4

    Detect mouse clicked on GUI

  5. 5

    Detect mouse clicked on GUI

  6. 6

    How to detect when mouse is outside of a certain circle?

  7. 7

    How to detect mouse clicked location on an image with custom shaped cursor in c++

  8. 8

    How to detect when mouse has stopped

  9. 9

    How to detect if a click has happened inside a box

  10. 10

    Android: detect if nearby device has certain app installed

  11. 11

    How to make a turtle object look at where the mouse has clicked

  12. 12

    How to detect user has clicked Don't Allow access to camera

  13. 13

    How do I detect which radio button has been clicked?

  14. 14

    How to detect if the user has clicked on "Quit" in a zenity list

  15. 15

    How to detect which item in a Matplotlib PathCollection has been clicked on?

  16. 16

    How to detect mouse clicks inside nested buttons in pygame

  17. 17

    OpenGL- C++ using mouse click to move shape to the clicked position

  18. 18

    OpenGL- C++ using mouse click to move shape to the clicked position

  19. 19

    Processing: How do you give a shape a value based on the mouse button clicked?

  20. 20

    How can I bring my shape that I painted with paintComponent and clicked with a mouse to front?

  21. 21

    Detect if input was touched (tablet) or clicked (mouse)

  22. 22

    Detect if input was touched (tablet) or clicked (mouse)

  23. 23

    How can I detect if an user has stopped walking using C# in my windows store app?

  24. 24

    Windows Phone 8: C# XAML how to detect shape collisions

  25. 25

    Windows Phone 8: C# XAML how to detect shape collisions

  26. 26

    How to detect if a button has been clicked OR an input's value has changed using jQuery?

  27. 27

    How do I draw a rectangle every time the mouse is clicked in a certain area?

  28. 28

    How do I draw a rectangle every time the mouse is clicked in a certain area?

  29. 29

    Detect a circular shape inside image in MATLAB

HotTag

Archive