鼠标事件不工作两次

Posted

技术标签:

【中文标题】鼠标事件不工作两次【英文标题】:Mouse event not working twice 【发布时间】:2017-05-09 07:19:24 【问题描述】:

我遇到了一个奇怪的问题。我有一个画布并绘制了一个复杂的结构: 边框内的网格内的图像。 我的图像是可以移动的。

我第一次拖动我的结构时效果很好,但第二次没有任何效果,无一例外,但没有任何效果。在我看来是鼠标捕捉的问题,但我无法捕捉到什么。

这是我的活动代码:

bool captured = false;
double x_shape, x_canvas, y_shape, y_canvas;
double x_shape_Origine, y_shape_Origine;
Border source = null;


int CountZ = 10;

private void shape_MoveButtonDown(object sender, MouseButtonEventArgs e)

    /// AWFULLY SOURCE WAS null. I don't understand Why it was working the    
    /// first time
    Mouse.Capture(source);
    captured = true;

    Image myImage = (Image)sender;
    Grid outerGrid = (Grid)myImage.Parent;
    source = (Border)outerGrid.Parent;

    Canvas.SetZIndex(source, CountZ++);
    x_shape = Canvas.GetLeft(source);
    y_shape = Canvas.GetTop(source);

    x_canvas = e.GetPosition(myCanvas).X;
    y_canvas = e.GetPosition(myCanvas).Y;




private void shape_MoveMoving(object sender, MouseEventArgs e)

    if (captured)
    
        double x = e.GetPosition(myCanvas).X;
        double y = e.GetPosition(myCanvas).Y;
        x_shape += x - x_canvas;
        Canvas.SetLeft(source, x_shape);
        x_canvas = x;
        y_shape += y - y_canvas;
        Canvas.SetTop(source, y_shape);
        y_canvas = y;
    


private void shape_MoveButtonUp(object sender, MouseButtonEventArgs e)

    Mouse.Capture(null);
    captured = false;



private void shape_MouseEnter(object sender, MouseEventArgs e)

    if (!captured)
    
        Border inner = (Border)sender;
        Grid parentInner = (Grid)inner.Parent;

        parentInner.Children[0].Visibility = Visibility.Visible;
        parentInner.Children[2].Visibility = Visibility.Visible;
        parentInner.Children[5].Visibility = Visibility.Visible;
        parentInner.Children[6].Visibility = Visibility.Visible;
        parentInner.Children[8].Visibility = Visibility.Visible;

        parentInner.Background = new SolidColorBrush(Colors.Red);
    


private void shape_MouseLeave(object sender, MouseEventArgs e)

    if (!captured)
    
        Grid outer = (Grid)sender;

        outer.Children[0].Visibility = Visibility.Hidden;
        outer.Children[2].Visibility = Visibility.Hidden;
        outer.Children[5].Visibility = Visibility.Hidden;
        outer.Children[6].Visibility = Visibility.Hidden;
        outer.Children[8].Visibility = Visibility.Hidden;

        outer.Background = null;
    

希望对你有意义

【问题讨论】:

【参考方案1】:

我有一个使用 WPF 和 Silverlight 进行 D&D 的完整示例。这里有完整的代码,希望对您有所帮助。

class MyWnd : Window
       
    public MyWnd()
    
        var c = new Canvas();
        c.Background = new SolidColorBrush(Colors.White);

        var rect = new Rectangle  Fill = new SolidColorBrush(Colors.Red), Width = 20, Height = 20 ;
        c.Children.Add(rect);

        this.Content = c;

        Canvas.SetLeft(rect, 0);
        Canvas.SetTop(rect, 0);

        rect.MouseLeftButtonDown+=Handle_MouseDown;
        rect.MouseLeftButtonUp+=Handle_MouseUp;
        rect.MouseMove+=Handle_MouseMove;
    


    bool isMouseCaptured;
    double mouseVerticalPosition;
    double mouseHorizontalPosition;

    public void Handle_MouseDown(object sender, MouseEventArgs args)
    
        var item = sender as FrameworkElement;
        mouseVerticalPosition = args.GetPosition(null).Y;
        mouseHorizontalPosition = args.GetPosition(null).X;
        isMouseCaptured = true;
        item.CaptureMouse();
    

    public void Handle_MouseMove(object sender, MouseEventArgs args)
    
        var item = sender as FrameworkElement;
        if (isMouseCaptured)
        

            // Calculate the current position of the object.
            double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
            double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
            double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
            double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);

            // Set new position of object.
            item.SetValue(Canvas.TopProperty, newTop);
            item.SetValue(Canvas.LeftProperty, newLeft);

            // Update position global variables.
            mouseVerticalPosition = args.GetPosition(null).Y;
            mouseHorizontalPosition = args.GetPosition(null).X;
        
    

    public void Handle_MouseUp(object sender, MouseEventArgs args)
    
        var item = sender as FrameworkElement;
        isMouseCaptured = false;
        item.ReleaseMouseCapture();
        mouseVerticalPosition = -1;
        mouseHorizontalPosition = -1;
    


void Main()

    var wnd = new MyWnd();
    wnd.ShowDialog();

【讨论】:

以上是关于鼠标事件不工作两次的主要内容,如果未能解决你的问题,请参考以下文章

鼠标事件

jQuery鼠标事件汇总

php超链接标签事件都有哪些

如何区分鼠标的单击和双击事件

java工作复习——鼠标事件——action类——单击鼠标左键不释放以及释放的操作

JQuery如何实现双击事件时不触发单击事件,解决鼠标单双击冲突问题