C# - WPF - 在画布上mouseMove事件将超载鼠标事件,以便单击事件不解雇 [英] C# - WPF - Mousemove event on canvas will overload the mouse events so click event is not fired

查看:1250
本文介绍了C# - WPF - 在画布上mouseMove事件将超载鼠标事件,以便单击事件不解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用形状和画布,我想做出点像mapeditor。当在我的画实际上选择的对象在上的一举一动鼠标位置画布画布上移动鼠标,那么,谁使用该程序可以看到它的样子,如果对象是摆在那里。

I use Shapes and Canvas, I want to make something like a mapeditor. When the mouse move over the canvas I draw the actually selected object to the canvas at the mouse position on every move, so who use the program can see how it will look like if the object is placed there.

和鼠标单击我当前的对象/位置添加到列表,其中包含这就需要在每次更新在画布上绘制的放置元素。

And on mouse click I add the current object/position to a list, which contains the placed elements which need to be drawn on the canvas in every update.

现在的问题是,如果鼠标移动处理程序被激活(绑定到画布上),然后单击事件是不永久开除,我需要不断点击大概有10次点击的元素放置。如果未绑定鼠标移动事件的点击完美的作品。

The problem is if the mouse move handler is active (binded to canvas) then the click event is not fired alway, I need to click continuously For about ten clicks to place the element. If the mouse move event is not binded then the click works perfectly.

我做了一个GIF演示我的问题。

I made a GIF to demonstrate my problem.

这里是当鼠标移动事件使用

Here is when the mouse move event is used

这里是在不

and here is when not

我想这是因为移动事件oveload事件处理,也没有资源来运行click事件。

I think it's because the move event oveload the event handling, and there is no resource to run the click event.

我怎么能同时使用两个事件?

How I could use the two event together?

修改

至于建议,我附上一些代码的例子。

As advised, I attach some code to the example.

我有一个名为画布上的模型 mapEditorModel 。这是对我们很重要的属性是 mapEditorModel.MapObjects 这是一个包含的元素需要被drawed画布列表。

I have a model for the canvas named mapEditorModel. The property which is important to us is the mapEditorModel.MapObjects which is a list containing the elements need to be drawed to the canvas.

该列表包含一个包装对象,它包含了很多关于elment,这对我们很重要的是,它包含了平局的预生成形状信息。

The list contains a wrapper object, its contains a lot of information about the elment, which is important to us is that it contains the prebuild shape for draw.

我有一个函数,它是画在画布上的elments:

I have a function which is draw the elments on the canvas:

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()的方法是这样的:

And I have an updateCanvas() method like this:

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

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

和这两个事件的方法是:

And the two event method is:

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

如果我评论在鼠标移动功能,所有的代码,那么我还是不能发生在第一次点击画布上的任何元素,也许是设计?

If I comment all the code in the mouse move function, then I still can't place any element on the canvas at the first click, so maybe is it by design?

推荐答案

下面是它工作正常,没有你提到的问题最少的代码示例:它可以快速添加形状,我可以点击。我建议你​​检查你的代码的分歧找到罪魁祸首 - 一,我不叫连续或updateCanvas,因为这类似不需要

Here's a minimal code sample which works properly and does not have the problem you mention: it can add shapes as fast as I can click. I suggest you inspect the differences with your code to find the culprit - for one, I don't continuously call updateCanvas or similar as this is not needed.

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:

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 );
    }
  }
}

这篇关于C# - WPF - 在画布上mouseMove事件将超载鼠标事件,以便单击事件不解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆