Mouse over location

G

Guest

Hello C-Sharpers,

Lets say i have a UserControl which draws a circle of radius r at
co-ordinates x,y. How can i tell if the cursor is currently inside or
outside of the circle?

thank you,
bk
 
S

Sorskoot

hi bk,

You'll have to calculate the distance between the mouseposition and the
coordinates of the circle. If this distance is smaller than r the
cursor is inside the circle.

Sorskoot.
 
R

Randolpho

Well, you have to override the OnMouseMove() method, naturally. You
could do some pythagorean calculations of your own in that method if
you wanted, but GraphicsPaths and Regions have the IsVisible() method
for checking if a point is contained (is "visible") within the
path/region, so if you just created a GraphicsPath that contains your
circle, you'd have an easier time.

First, create a GraphicsPath object and keep it as a member variable of
your user control. Call AddEllipse() on the GraphicsPath in the same
way you originally drew it on your UserControl using DrawEllipse(). In
your OnPaint() method or Paint Event handler method, call DrawPath() on
your GraphicsPath object rather than DrawEllipse().

For the actual testing, override the OnMouseMove() method, then call
IsVisible(e.X, e.Y) on your GraphicsPath to determine if the point is
inside your circle.

E.g:

public class CircleTest : UserControl
{
private GraphicsPath MyCircle;

public CircleTest()
{
MyCircle = new GraphicsPath();
MyCircle.AddEllipse(5, 5, 5, 5); // circle of radius 2.5 at (5,5)
}
private override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawPath(Pens.Black, MyCircle);
}
private override void OnMouseMove(MouseEventArgs e)
{
if(MyCircle.IsVisible(e.X, e.Y))
{
// hit-test positive... the cursor is over the circle.
}
}
}
 
L

Lyny

Try this... Create a new (C# windows) project and put this code in the form
cs file:
using System;

using System.Drawing;

using System.Windows.Forms;

namespace ExempleGraphique

{

public partial class Form1 : Form

{

Point MyCircleCenter = new Point(100, 50);

Size MyCircleRay = new Size(75, 75); // Must be the same values to generate
a circle.

public Form1() { InitializeComponent(); }

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

g.Clear(this.BackColor);

base.OnPaint(e);

// Drawing Circle contained in x=0-150 & y=0-150

// Said simply: center = 75,75, ray=75

Rectangle MyCirclePosition = new Rectangle(

MyCircleCenter.X - MyCircleRay.Width/2,

MyCircleCenter.Y - MyCircleRay.Height/2,

MyCircleRay.Width,

MyCircleRay.Height);

g.DrawEllipse(Pens.Red, MyCirclePosition);

}

private void Form1_MouseMove(object sender, MouseEventArgs e)

{

// If distance from center is higher than circle ray, it's outside the
circle.

Boolean IsInCircle = (

(e.X - MyCircleCenter.X) * (e.X - MyCircleCenter.X) +

(e.Y - MyCircleCenter.Y) * (e.Y - MyCircleCenter.Y) )

< (MyCircleRay.Height * MyCircleRay.Width /4);

if (IsInCircle) { txtMessage.Text = "In the cercle"; }

else { txtMessage.Text = "Out of the cercle"; }

}

}

}
 
O

Otis Mukinfus

Well, you have to override the OnMouseMove() method, naturally. You
could do some pythagorean calculations of your own in that method if
you wanted, but GraphicsPaths and Regions have the IsVisible() method
for checking if a point is contained (is "visible") within the
path/region, so if you just created a GraphicsPath that contains your
circle, you'd have an easier time.

First, create a GraphicsPath object and keep it as a member variable of
your user control. Call AddEllipse() on the GraphicsPath in the same
way you originally drew it on your UserControl using DrawEllipse(). In
your OnPaint() method or Paint Event handler method, call DrawPath() on
your GraphicsPath object rather than DrawEllipse().

For the actual testing, override the OnMouseMove() method, then call
IsVisible(e.X, e.Y) on your GraphicsPath to determine if the point is
inside your circle.

E.g:

public class CircleTest : UserControl
{
private GraphicsPath MyCircle;

public CircleTest()
{
MyCircle = new GraphicsPath();
MyCircle.AddEllipse(5, 5, 5, 5); // circle of radius 2.5 at (5,5)
}
private override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawPath(Pens.Black, MyCircle);
}
private override void OnMouseMove(MouseEventArgs e)
{
if(MyCircle.IsVisible(e.X, e.Y))
{
// hit-test positive... the cursor is over the circle.
}
}
}

Bravo!

I had to do that once to track aircraft movement over states in the USA. Each
state was a GraphicsPath drawn using latitude, longitude points. That
application was a VB 5.0 app.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top