2010-03-09 32 views

Respuesta

25

Tener un vistazo a GetChildAtPoint. Tendrá que hacer un trabajo adicional si los controles están contenidos en un contenedor, consulte Control.PointToClient.

+0

¡Eso es todo! ¡Gracias! – Poma

+1

+1 para Control.PointToClient –

1

¿Qué pasa con la definición de un evento en el ratón sobre cada botón en que asigna el botón remitente a una variable pública de un tipo de botón

+0

En realidad no son botones, son complementos de terceros;) – Poma

1

que podría hacerlo de varias maneras:

  1. Escucha la MouseEnter caso de los controles del formulario. El parámetro "emisor" le dirá qué control levantó el evento.

  2. Obtenga la posición del cursor usando System.Windows.Forms.Cursor.Location y asígnela a las coordenadas de su formulario usando Form.PointToClient(). A continuación, puede pasar el punto al Form.GetChildAtPoint() para buscar el control en ese punto.

Andrew

2
// This getYoungestChildUnderMouse(Control) method will recursively navigate a  
// control tree and return the deepest non-container control found under the cursor. 
// It will return null if there is no control under the mouse (the mouse is off the 
// form, or in an empty area of the form). 
// For example, this statement would output the name of the control under the mouse 
// pointer (assuming it is in some method of Windows.Form class): 
// 
// Console.Writeline(ControlNavigatorHelper.getYoungestChildUnderMouseControl(this).Name); 


    public class ControlNavigationHelper 
    { 
     public static Control getYoungestChildUnderMouse(Control topControl) 
     { 
      return ControlNavigationHelper.getYoungestChildAtDesktopPoint(topControl, System.Windows.Forms.Cursor.Position); 
     } 

     private static Control getYoungestChildAtDesktopPoint(Control topControl, System.Drawing.Point desktopPoint) 
     { 
      Control foundControl = topControl.GetChildAtPoint(topControl.PointToClient(desktopPoint)); 
      if ((foundControl != null) && (foundControl.HasChildren)) 
       return getYoungestChildAtDesktopPoint(foundControl, desktopPoint); 
      else 
       return foundControl; 
     } 
    } 
13

Tal GetChildAtPoint y PointToClient es la primera idea para la mayoría de la gente. También lo usé primero. Pero, GetChildAtPoint no funciona correctamente con controles invisibles o superpuestos. Aquí está mi código que funciona bien y maneja esas situaciones.

using System.Drawing; 
using System.Windows.Forms; 

public static Control FindControlAtPoint(Control container, Point pos) 
{ 
    Control child; 
    foreach (Control c in container.Controls) 
    { 
     if (c.Visible && c.Bounds.Contains(pos)) 
     { 
      child = FindControlAtPoint(c, new Point(pos.X - c.Left, pos.Y - c.Top)); 
      if (child == null) return c; 
      else return child; 
     } 
    } 
    return null; 
} 

public static Control FindControlAtCursor(Form form) 
{ 
    Point pos = Cursor.Position; 
    if (form.Bounds.Contains(pos)) 
     return FindControlAtPoint(form, form.PointToClient(pos)); 
    return null; 
} 

Esto le dará el control justo debajo del cursor.

Cuestiones relacionadas