|
-
October 5th, 2007, 05:53 PM
#1
get nearest control to mouse cursor
I am trying to get the nearest control to my mouse pointer in my app but somehow i can't manage to access the right thing. Project: http://files.filefront.com/MyMoO3rar.../fileinfo.html
EDIT: figured out this far that it returns the index from panel1 Controls so i should divide the index by 3, but still it doesn't compare the right star control location altough i'm giving it only star Locations.
Code:
if (e.KeyData == Keys.Space && starMap1.MouseIsOverCtrl)
{
int distance, starIndex = -1, lastNearest = 0xFFFF;
Point mousePoint = Cursor.Position, ctrlPoint;
Console.WriteLine("mouse location {0}", mousePoint);
foreach(Star star in starMap1.starArray)
{
ctrlPoint = star.PointToScreen(star.Location);
distance = Convert.ToInt32(Math.Sqrt(Math.Pow((mousePoint.X - ctrlPoint.X), 2F)+ Math.Pow((mousePoint.Y - ctrlPoint.Y), 2F)));
if (distance < lastNearest)
{
lastNearest = distance;
Console.WriteLine("lastNearest {0}", lastNearest);
starIndex = star.Parent.Controls.GetChildIndex(star);
}
}
Console.WriteLine("starIndex {0}", starIndex);
}
Last edited by w0lfshad3; October 6th, 2007 at 09:13 AM.
-
October 6th, 2007, 09:27 AM
#2
Re: get nearest control to mouse cursor
Success:
Code:
if (e.KeyData == Keys.Space && starMap1.MouseIsOverCtrl)
{
int distance, starIndex = -1, lastNearest = 0xFFFF;
Point mousePoint = Cursor.Position, ctrlPoint;
mousePoint = this.Controls["starMap1"].Controls["panel1"].PointToClient(mousePoint);
Console.WriteLine("mouse location {0}", mousePoint);
foreach(Star star in starMap1.starArray)
{
ctrlPoint = star.Location;
distance = Convert.ToInt32(Math.Sqrt(Math.Pow((mousePoint.X - ctrlPoint.X), 2F)+ Math.Pow((mousePoint.Y - ctrlPoint.Y), 2F)));
if (distance < lastNearest)
{
lastNearest = distance;
Console.WriteLine("lastNearest {0}", lastNearest);
starIndex = star.Parent.Controls.GetChildIndex(star) / 3;
}
}
Console.WriteLine("starIndex {0}", starIndex);
}
-
October 6th, 2007, 10:00 AM
#3
Good
So, you made it work. Good.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|