Click here to Skip to main content
16,019,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have a little problem. Let me share with you.

Lets say I have a pictureBox, and I Draw en ellipse into it. While I'm moving the mouse over the pictureBox, When I reach the area of the ellipse, There will appear a messageBox that consists "id1" or something like that.

How can I know that the mouse is on ellipse?

Is it possible? If so, How can I achieve this?

Need links or examples.

My best regards...
Posted
Updated 7-Jul-11 1:24am
v2

Handle the PictureBox.MouseMove event: the MouseEventArgs parameter will give you the location of the mouse pointer:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
    Point mouseIsAt = e.Location;
    }
To actually check if this is on (or even in) your oval is harder:
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(new Rectangle(new Point(10, 10), new Size(100, 200)));
Region reg = new Region(gp);
if (reg.IsVisible(mouseIsAt))
    {
    Console.WriteLine("Yes");
    }
else
    {
    Console.WriteLine("No");
    }




"Hi sir.
That seems a nice and sharp answer, but I don't get something in code.
gp.AddEllipse(new Rectangle(new Point(10, 10), new Size(100, 200)));
What does this code actually do? Could you explain it a little for me?
My best regards..."


In order to test if a point is inside an ellipse, you can't just rely on the bounding rectangle - the corners are "missing" from the actual ellipse. And there is no way to get the actual points that the ellipse is made of: they don't really exist except when drawn!

The only way to do it is to set up a region which consists of the actual ellipse - which means setting up the ellipse as a GraphicsPath object first.
The example I gave creates a new Graphics path, and adds an ellipse to it: since I don't know how you are cretaing your oval, I couldn't refer to your code! However you are drawing the ellipse on your picture box, use the same code to add it to a GraphicsPath, and then create the Region from that - you will probably want this as a class level variable, rather than local, as well, but I illustrated it with local to keep the code together.
 
Share this answer
 
v2
Comments
[no name] 7-Jul-11 7:26am    
Valid Answer. My 5.
Un_NaMeD 7-Jul-11 8:01am    
Hi sir.

That seems a nice and sharp answer, but I don't get something in code.

gp.AddEllipse(new Rectangle(new Point(10, 10), new Size(100, 200)));

What does this code actually do? Could you explain it a little for me?
My best regards...
OriginalGriff 7-Jul-11 8:11am    
Answer updated
Un_NaMeD 7-Jul-11 8:24am    
I guess I understand it.
I'll give it a try.
Thank you very much...
OriginalGriff 7-Jul-11 8:28am    
You're welcome!
Hi, You can get mouse position on mousemove event.
C#
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
       {
          label1.Text=e.Location + "  " + e.X + " " + e.Y;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900