Click to See Complete Forum and Search --> : get nearest control to mouse cursor


w0lfshad3
October 5th, 2007, 05:53 PM
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/;8725371;/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.






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);

}

w0lfshad3
October 6th, 2007, 09:27 AM
Success:


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);
}

visharad
October 6th, 2007, 10:00 AM
So, you made it work. Good.