3

这是场景(简化):我在窗口上有一个控件(比如说,一个矩形)。我挂钩了 MouseMove 事件以使其启动拖放操作。然后在 MouseDown 事件中,我让它动画,向右移动 50 像素。但是,当我在 Rectangle 上按住鼠标时,控件会移动大约一个像素,然后暂停。只有当我移动鼠标时,动画才会继续。有谁知道为什么以及如何解决这个问题?非常感谢!!

这是重现此问题的源代码:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void rectangle1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DragDrop.DoDragDrop(this, new DataObject(), DragDropEffects.Copy);
        }
    }

    private void rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ThicknessAnimation animation = new ThicknessAnimation();
        Thickness t = rectangle1.Margin;
        t.Left += 50;
        animation.To = t;
        animation.Duration = new Duration(TimeSpan.FromSeconds(0.25));
        rectangle1.BeginAnimation(Rectangle.MarginProperty, animation);
    }
}

如果您想要 Window1.xaml:

<Window x:Class="DragDropHaltingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Rectangle Margin="12,12,0,0" Name="rectangle1" Stroke="Black" Fill="Blue" Height="29" HorizontalAlignment="Left" VerticalAlignment="Top" Width="31" MouseMove="rectangle1_MouseMove" MouseLeftButtonDown="rectangle1_MouseLeftButtonDown" />
</Grid>

4

1 回答 1

3

我偶然发现了同样的问题;我的拖放源沿着动画路径移动。如果我将鼠标放在拖动源的路径上并按住鼠标左键,当源触摸鼠标时动画将停止。当我释放鼠标按钮或移动鼠标时,动画将继续。(有趣的是,如果我打开 Windows 任务管理器并定期刷新进程列表,动画也会继续!)

情况分析

据我了解,WPF 动画会在CompositionTarget.Rendering事件中更新。在正常情况下,它会在每次屏幕刷新时触发,可能是每秒 60 次。就我而言,当我的拖动源在鼠标下移动时,它会触发MouseMove事件。在那个事件处理程序中,我调用DragDrop.DoDragDrop. 此方法阻塞 UI 线程,直到拖放完成。当 UI 线程进入DragDrop.DoDragDrop时,则CompositionTarget.Rendering不会触发。从某种意义上说,触发事件的线程在拖放时被阻塞是可以理解的。但!如果您移动鼠标(也许其他类型的输入也可以解决问题),则会MouseMove触发一个事件。它在拖放操作中仍被阻止的同一 UI 线程中触发。在MouseMove被解雇和处理后,CompositionTarget.Rendering在仍被“阻塞”的 UI 线程中继续定期触发。

我从下面两个堆栈跟踪的比较中收集到这一点。我根据对堆栈帧含义的理解不足对堆栈帧进行了分组。第一个堆栈跟踪取自我的MouseMove事件处理程序,而没有拖放操作处于活动状态。这是“通常情况”。

  • C) 事件特定处理(鼠标事件)
    System.Windows.Input.MouseEventArgs.InvokeEventHandler
    :
    System.Windows.Input.InputManager.HitTestInvalidatedAsyncCallback
  • B) 消息调度
    System.Windows.Threading.ExceptionWrapper.InternalRealCall
    :
    MS.Win32.HwndSubclass.SubclassWndProc
    MS.Win32.UnsafeNativeMethods.DispatchMessage
  • A) 应用程序主循环
    System.Windows.Threading.Dispatcher.PushFrameImpl
    :
    System.Threading.ThreadHelper.ThreadStart

第二个堆栈跟踪是在拖放操作期间从我的CompositionTarget.Rendering事件处理程序中获取的。帧组 A、B 和 C 与上述相同。

  • F) 事件特定处理(渲染事件) System.Windows.Media.MediaContext.RenderMessageHandlerCore
    System.Windows.Media.MediaContext.AnimatedRenderMessageHandler
  • E) 消息调度(与 B 相同)
    System.Windows.Threading.ExceptionWrapper.InternalRealCall
    :
    MS.Win32.HwndSubclass.SubclassWndProc
  • D)Drag'n'drop(在我的事件处理程序中启动)
    MS.Win32.UnsafeNativeMethods.DoDragDrop

    System.Windows.DragDrop.DoDragDrop
  • C) 特定事件处理(鼠标事件)
  • B) 消息发送
  • A) 应用程序主循环

因此,WPF 在消息调度 (B) 中运行消息调度 (E)。这解释了如何DragDrop.DoDragDrop在 UI 线程上调用并阻塞线程时,我们仍然能够在同一线程上运行事件处理程序。我无法想象为什么内部消息调度没有从拖放操作开始连续运行以执行CompositionTarget.Rendering更新动画的常规事件。

可能的解决方法

触发一个额外的 MouseMove 事件

DoDragDrop正如 VlaR 建议的那样,一种解决方法是在执行时触发鼠标移动事件。他的链接似乎假设我们正在使用表单。对于 WPF,细节有点不同。按照MSDN 论坛上的解决方案(由原始发布者提供?),此代码可以解决问题。我修改了源代码,使其可以在 32 位和 64 位下工作,并且还可以避免计时器在触发之前被 GC 处理的罕见机会。

[DllImport("user32.dll")]
private static extern void PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

private static Timer g_mouseMoveTimer;

public static DragDropEffects MyDoDragDrop(DependencyObject source, object data, DragDropEffects effects, Window callerWindow)
{
    var callerHandle = new WindowInteropHelper(callerWindow).Handle;
    var position = Mouse.GetPosition(callerWindow);
    var WM_MOUSEMOVE = 0x0200u;
    var MK_LBUTTON = new IntPtr(0x0001);
    var lParam = (IntPtr)position.X + ((int)position.Y << (IntPtr.Size * 8 / 2));
    if (g_mouseMoveTimer == null) {
        g_mouseMoveTimer = new Timer { Interval = 1, AutoReset = false };
        g_mouseMoveTimer.Elapsed += (sender, args) => PostMessage(callerHandle, WM_MOUSEMOVE, MK_LBUTTON, lParam);
    }
    g_mouseMoveTimer.Start();
    return DragDrop.DoDragDrop(source, data, effects);
}

使用此方法而不是DragDrop.DoDragDrop将使动画继续进行而不会停止。但是,似乎并不能尽快DragDrop了解正在进行的拖拽。因此,您冒着一次输入多个拖动操作的风险。调用代码应该像这样防范它:

private bool _dragging;

source.MouseMove += (sender, args) => {
    if (!_dragging && args.LeftButton == MouseButtonState.Pressed) {
        _dragging = true;
        MyDoDragDrop(source, data, effects, window);
        _dragging = false;
    }
}

还有另一个故障。由于某种原因,在开始描述的情况下,只要鼠标左键按下,鼠标没有移动,并且放置源正在移动,鼠标光标就会在普通箭头光标和拖动光标之间闪烁光标。

仅当鼠标真正移动时才允许拖动

我尝试的另一个解决方案是在开头描述的情况下禁止从开始拖放。为此,我连接了一些 Window 事件来更新状态变量,所有代码都可以压缩到这个初始化例程中。

/// <summary>
/// Returns a function that tells if drag'n'drop is allowed to start.
/// </summary>
public static Func<bool> PrepareForDragDrop(Window window)
{
    var state = DragFilter.MustClick;
    var clickPos = new Point();
    window.MouseLeftButtonDown += (sender, args) => {
        if (state != DragFilter.MustClick) return;
        clickPos = Mouse.GetPosition(window);
        state = DragFilter.MustMove;
    };
    window.MouseLeftButtonUp += (sender, args) => state = DragFilter.MustClick;
    window.PreviewMouseMove += (sender, args) => {
        if (state == DragFilter.MustMove && Mouse.GetPosition(window) != clickPos)
            state = DragFilter.Ok;
    };
    window.MouseMove += (sender, args) => {
        if (state == DragFilter.Ok)
            state = DragFilter.MustClick;
    };
    return () => state == DragFilter.Ok;
}

调用代码将如下所示:

public MyWindow() {
    var dragAllowed = PrepareForDragDrop(this);
    source.MouseMove += (sender, args) => {
        if (dragAllowed()) DragDrop.DoDragDrop(source, data, effects);
    };
}

这适用于易于重现的情况,即开始拖动,因为当鼠标左键按下时,动画将拖动源移动到静止的鼠标光标上。然而,这并不能避免根本问题。如果您单击拖动源并移动得太少以至于仅触发一个鼠标事件,则动画将停止。幸运的是,这在实际实践中不太可能发生。此解决方案的好处是不直接使用 Win32 API,也不必涉及后台线程。

DoDragDrop从后台线程调用

不工作。DragDrop.DoDragDrop必须从 UI 线程调用并阻止它。调用使用调度程序调用的后台线程无济于事DoDragDrop

于 2013-04-09T17:52:39.213 回答