CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    50

    Question 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.

  2. #2
    Join Date
    May 2005
    Posts
    50

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

  3. #3
    Join Date
    Oct 2006
    Posts
    216

    Thumbs up 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
  •  





Click Here to Expand Forum to Full Width

Featured