如何在形状上实现拖曳delta

时间:2011-09-03 02:26:47

标签: wpf events drag raise

如何在Shape上实现拖动增量,我有以下代码:

void Connector_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ClickCount == 1)
        {
            this.Focus();
            this.CaptureMouse();
            this.RaiseEvent(new DragStartedEventArgs(0,0));
            initMousePoint = e.GetPosition(this);
        }
        e.Handled = true;
    }
    void Shape2_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        currMousePoint = e.GetPosition(this);
        if (this.IsMouseCaptured)
        {
                this.RaiseEvent(new DragDeltaEventArgs(0,0);
        }
    }
    void Shape2_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.ReleaseMouseCapture();
    }

现在对于DragDeltaEvent args,我需要计算mousemove中的拖动并将其传递给事件,这也是提升事件的正确方法。如果这样可行,那么我应该只订阅拖动增量事件并将其用作拇指?注意,我不想用拇指模板形状,只要这个答案对我不起作用。


注意chagnes,关于获取鼠标的位置,这个我觉得不行,因为它获取相对于元素的位置,而不是包含面板,所以我不认为我能够通过这种方式找到拖动距离。

1 个答案:

答案 0 :(得分:2)

我用以下方法解决了它:

currMousePoint = e.GetPosition(this);
double dragHorizontal = currMousePoint.X - initMousePoint.X;
double dragVertical = currMousePoint.Y - initMousePoint.Y;
//Set the new canvas top and left proeprties here.