canvas教程

canvas上的Mousemove事件将重载鼠标事件,因此点击事件不会触发(2)

字号+ 作者:H5之家 来源:H5之家 2017-07-29 12:04 我要评论( )

我有一个函数在画布上绘制的元素: private void DrawElementOnCanvas(MapElementContainer item) { Rectangle shape = item.Shape; CanvasElement.Children.Add(shape); Canvas.SetLeft(shape,item.Position

我有一个函数在画布上绘制的元素:



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 c $ c>方法如下:



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

foreach(mapEditorModel.MapObjects中的MapElementContainer项)
{
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();
}


EDIT 2



如果我注释鼠标移动函数中的所有代码,那么我仍然不能在画布上第一次点击任何元素,所以也许是设计?


解决方案

这里是一个最小的代码示例,它可以正常工作,没有你提到的问题:可以点击。我建议你检查与你的代码的差异找到罪魁祸首 - 一个,我不连续调用updateCanvas或类似,因为这是不需要的。



xaml: / p>

< Window x:Class =“WpfApplication1.MainWindow”
xmlns =“。 com / winfx / 2006 / xaml / presentation“
xmlns:x =”“
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;
使用System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Shapes;

命名空间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>形状;
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);
}
}
}

本文地址:IT屋 » C# - WPF - canvas上的Mousemove事件将重载鼠标事件,因此点击事件不会触发

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 用Javascript和canvas实现的涂鸦板,似乎不支持IE8及以下浏览器

    用Javascript和canvas实现的涂鸦板,似乎不支持IE8及以下浏览器

    2017-07-29 13:00

  • 当屏幕调整大小时调整画布大小

    当屏幕调整大小时调整画布大小

    2017-07-29 08:00

  • Canvas 绘制粒子动画背景

    Canvas 绘制粒子动画背景

    2017-07-27 12:05

  • HTML5 Canvas 爆炸动画特效

    HTML5 Canvas 爆炸动画特效

    2017-07-27 11:10

网友点评