【问题标题】:How to get the element the mouse pointer is directly over如何获取鼠标指针直接位于的元素
【发布时间】:2018-10-16 16:24:59
【问题描述】:

我有一个画布,可以在其中拖放项目。我想对 drop 事件设置一些限制。例如,如果项目 1 已被拖放到画布上,我希望只有当鼠标指针直接位于项目 1 上时才能将项目 2 拖放到画布上。

我使用了 Mouse.DirectlyOver 但它返回 null。那么,如何在 drop 事件上直接获取鼠标指针直接在上面的元素?

注意:我正在使用 C# WPF 进行编程

谢谢!

【问题讨论】:

    标签: c# wpf mouseevent


    【解决方案1】:

    这个答案认为您使用的是winforms是理所当然的。

    实现此目的的一种方法是获取光标的 X/Y 坐标以检查它是否在 item1 内。可以使用DragDrop事件的DragEventArgs获取事件触发时的鼠标坐标

    private void canvas_DragDrop(object sender, DragEventArgs e)
    {
        //Get cursor x/y coordinates
        int cursorPositionX = e.X;
        int cursorPositionY = e.Y;
    
        //Check if it is inside item 1
        if (cursorPositionX > item1.Location.X && cursorPositionX < item1.Location.X + item1.Width && cursorPositionY > item1.Location.Y && cursorPositionY < item1.Location.Y + item1.Height)
        {
            //The mouse is directly above item1
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多