【问题标题】:Mouse events for captured element stop firing while dragging拖动时捕获元素的鼠标事件停止触发
【发布时间】:2016-07-29 08:33:47
【问题描述】:

我有一些代码在屏幕上绘制一些点,然后允许在按住鼠标左键的同时拖动它们。除了不断地鼠标事件停止触发并且被拖动的点停止移动之外,这种方法还可以工作。

因为所有事件都停止被捕获(出于某种未知原因),这意味着用户可以释放鼠标左键而不会发生任何事情。

奇怪的是,用户可以将鼠标重新定位在该点上,它会再次开始拖动,而无需按住鼠标左键。这会导致非常糟糕的用户体验。

发生了什么事?

pinPoint.MouseLeftButtonDown += Point_MouseLeftButtonDown;
pinPoint.MouseLeftButtonUp += Point_MouseLeftButtonUp;
pinPoint.MouseMove += Point_MouseMove;

.
.
.

void Point_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ((UIElement)sender).CaptureMouse();
    if (_isDragging == false)
    {
        _isDragging = true;
    }
}

void Point_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    _isDragging = false;
    ((UIElement)sender).ReleaseMouseCapture();
}

void Point_MouseMove(object sender, MouseEventArgs e)
{
    //where the point gets moved and some other logic
    //possibly this logic takes too long?
}

【问题讨论】:

    标签: c# wpf drag-and-drop mouseevent mousecapture


    【解决方案1】:

    我找到了一个可行的解决方案。不过,我仍然不知道为什么它会不断丢失 MouseCapture。

    pinPoint.LostMouseCapture += Point_LostMouseCapture;
    
    .
    .
    .
    
    void Point_LostMouseCapture(object sender, MouseEventArgs e)
    {
        //if we lost the capture but we are still dragging then just recapture it
        if (_isDragging)
        {
            ((UIElement)sender).CaptureMouse();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-27
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      相关资源
      最近更新 更多