473,508 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mouse over location

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
May 2 '06 #1
4 9115
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.

May 2 '06 #2
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.
}
}
}

May 2 '06 #3
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"; }

}

}

}

<bk> a écrit dans le message de news:
%2****************@TK2MSFTNGP04.phx.gbl...
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

May 2 '06 #4
On 2 May 2006 07:02:06 -0700, "Randolpho" <ra*******@gmail.com> wrote:
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
May 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2888
by: Jay | last post by:
I need to be able to move a Panel on a windows Form at run time using the mouse. I have tried adding a MouseDown event handler to the Panel and then within the MouseDown handler adding a MouseMove...
2
21401
by: Robin Senior | last post by:
Hi, I'm trying to drag and drop onto a Panel on my form. The panel is inside a groupBox, which of course is inside my form. When dropping the item onto my Panel, I want it to appear at that...
4
2228
by: Raj Chudasama | last post by:
I have a controls similiar to the windows calculator. (Please press some buttons on your calculator to see what i am talking about) So when u hover over a button it will change the state (it...
5
3683
by: Charles Law | last post by:
Sorry for reposting this question, but I did not get a single answer last time, and I'm sure you guys must have some thoughts on the matter. I have a user control which can be dragged and dropped...
3
2055
by: jcrouse | last post by:
I have created a form designer type application (with a lot of you peoples helpJ). It has label controls that are draggable at runtime. The user is also allowed to change some properties such as...
13
3109
by: Lars Netzel | last post by:
Hi! I have a round area which I want to be able to move the mouse over and fire off events... how do I do that? I have drawn a FillPie Graphics and I feel that there has to be a way of...
2
4989
by: Sam | last post by:
Hi, I can't figure out how to detect when my mouse cursor leaves a panel control. It should not trigger the event (or do anything) when the mouse leave the panel but still is over a control that...
1
1754
by: beatsoup | last post by:
Is there a way to find out the location of the mouse when an event object is not handy? For example, let's say I want to wake up in 1 second and check where the mouse is. The function called by the...
2
19571
by: quickcur | last post by:
Hi, I have html like this: <div id="myCanvas" style="border:10px, black;position:relative;height:250px;width:100%;"> <img id="p" src="p.jpg"> </div> When user click the mosue, I would like ...
5
7774
by: EggHead | last post by:
Hi all, My development environment is VB 6.0, and my OS is winxp. I would like to know how to find the mouse location at a control when the mouse is point at that control. The problem is that the...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.