本帖最后由 lgjgsslgjgss 于 2012-07-08 16:07:16 编辑

解决方案 »

  1.   


    定义一个类c:有x1,x2,y1,y2几个属性
    formload事件中遍历所有的Button控件,将每个控件的position.x,position.y及clientsize.with,clientsize.height记录到一个hashtable或字典中
    key为控件名
    value为新类c
    在你form中的mousemove事件中,根据e.X,e.Y获取当前位置
    去遍历你在hashtable或字典中的类c的值c.x1->c.x2及c.y1->c.y2,看在哪个之中,
    则这些控件就是你要获取的
      

  2.   

    这个太复杂了。我现在找到了一个很简单的方法。不过,还没有最终调通。下面的代码,可以直接获取鼠标位置上的元素。不过,我还要判断,这个元素,是不是在一个Buton里面。现在,还没有成功,不知道怎么判断?System.Windows.Input.Mouse.DirectlyOver
      

  3.   

    解决了。 
                bool InButton = false;   //如果当前鼠标在一个Button里面,那么,最后变成true;
                System.Windows.UIElement u = System.Windows.Input.Mouse.DirectlyOver as System.Windows.UIElement;
                while (u != null)  
                {
                    var correctlyTyped = u as System.Windows.Controls.Button;
                    if (correctlyTyped != null)
                    {
                        InButton  = true;
                    }
                    u = VisualTreeHelper.GetParent(u) as UIElement;
                }