簡體   English   中英

C# - WPF - 畫布上的Mousemove事件將使鼠標事件重載,因此不會觸發click事件

[英]C# - WPF - Mousemove event on canvas will overload the mouse events so click event is not fired

我使用Shapes和Canvas,我想制作像mapeditor這樣的東西。 當鼠標在畫布上移動時,我會在每次移動時將實際選擇的對象繪制到鼠標位置的畫布上,因此使用該程序的人可以看到如果將對象放在那里它會是什么樣子。

在鼠標單擊時,我將當前對象/位置添加到列表中,該列表包含每次更新時需要在畫布上繪制的放置元素。

問題是如果鼠標移動處理程序處於活動狀態(綁定到畫布)然后單擊事件不會被觸發,我需要連續單擊以進行大約十次單擊以放置元素。 如果鼠標移動事件沒有綁定,則點擊工作完美。

我做了一個GIF來證明我的問題。

這是使用鼠標移動事件的時間

而這時候不是

我認為這是因為move事件超載了事件處理,並且沒有資源來運行click事件。

我怎么能一起使用這兩個事件?

編輯

按照建議,我將一些代碼附加到示例中。

我有一個名為mapEditorModel的畫布模型。 對我們來說很重要的屬性是mapEditorModel.MapObjects ,它是一個包含需要繪制到畫布的元素的列表。

該列表包含一個包裝器對象,它包含很多關於元素的信息,這對我們來說很重要,它包含了用於繪制的預構建形狀。

我有一個功能,它在畫布上繪制元素:

private void DrawElementOnCanvas(MapElementContainer item)
    {
        Rectangle shape = item.Shape;

        CanvasElement.Children.Add(shape);
        Canvas.SetLeft(shape, item.Position.X);
        Canvas.SetTop(shape, item.Position.Y);    
    }

我有一個像這樣的updateCanvas()方法:

private void updateCanvas()
    {
        CanvasElement.Children.RemoveRange(0, CanvasElement.Children.Count);

        foreach (MapElementContainer item in mapEditorModel.MapObjects)
        {
            DrawElementOnCanvas(item);   
        }
        //CollisionDetection();
    }

這兩種事件方法是:

private void CanvasElement_MouseMove(object sender, MouseEventArgs e)
    {
        updateCanvas();

        MapElementContainer mapObject = new MapElementContainer();

        mapObject.Position = e.GetPosition((Canvas)sender);
        mapObject.MapElement = new ContainerMapObject();
        mapObject.CurrentRotateDegree = mapEditorModel.CurrentRotateDegree;
        mapObject.Shape = BuildShape(mapObject);

        DrawElementOnCanvas(mapObject);   
    }

private void CanvasElement_MouseDown(object sender, MouseButtonEventArgs e)
    {
        MapElementContainer mapObject = new MapElementContainer();

        mapObject.Position = e.GetPosition((Canvas)sender);
        mapObject.MapElement = new ContainerMapObject();
        mapObject.CurrentRotateDegree = mapEditorModel.CurrentRotateDegree;
        mapObject.Shape = BuildShape(mapObject);

        mapEditorModel.MapObjects.Add(mapObject);

        updateCanvas();
    }

編輯2

如果我評論鼠標移動功能中的所有代碼,那么我仍然無法在第一次點擊時在畫布上放置任何元素,所以也許它是設計的?

這是一個最小的代碼示例,它可以正常工作並且沒有您提到的問題:它可以盡可能快地添加形狀。 我建議你檢查你的代碼的差異,找到罪魁禍首 - 對於一個,我不會連續調用updateCanvas或類似的,因為這是不需要的。

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight">
  <Canvas x:Name="canvas" Background="AntiqueWhite" Width="1024" Height="768"
          MouseMove="Canvas_MouseMove" MouseDown="Canvas_MouseDown" />
</Window>

xaml.cs:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication1
{
  public class Item
  {
    private readonly Rectangle shape;

    public Item( Canvas canvas )
    {
      shape = new Rectangle { Width = 50, Height = 50, Fill = Brushes.Black };
      canvas.Children.Add( shape );
      SetPosition( 0.0, 0.0 );
    }

    public void SetPosition( double x, double y )
    {
      Canvas.SetLeft( shape, x );
      Canvas.SetTop( shape, y );
    }
  }

  public partial class MainWindow : Window
  {
    private readonly IList<Item> shapes;
    private Item currentMovingShape;

    public MainWindow()
    {
      InitializeComponent();
      shapes = new List<Item>();
      InitMovingShape();
    }

    private void InitMovingShape()
    {
      currentMovingShape = new Item( canvas );
    }

    private void SetMovingShapePosition( MouseEventArgs e )
    {
      var pos = e.GetPosition( canvas );
      currentMovingShape.SetPosition( pos.X, pos.Y );
    }

    private void Canvas_MouseMove( object sender, MouseEventArgs e )
    {
      SetMovingShapePosition( e );
    }

    private void Canvas_MouseDown( object sender, MouseButtonEventArgs e )
    {
      shapes.Add( currentMovingShape );
      InitMovingShape();
      SetMovingShapePosition( e );
    }
  }
}

也許MouseDown事件在MapElementContainer類中處理,永遠不會到達CanvasElement? 在第二個視頻中,您總是點擊一個空白區域。 在您的第一個視頻中,鼠標下始終存在MapElementContainer。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM