我在处理画布上的鼠标事件时遇到问题。我想用鼠标绘制它,并且我已经提出了这些事件处理程序,但是当我开始绘制时它们不会执行任何操作。WPF - 在画布上绘制鼠标事件
private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
}
private void paintSurface_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Line line = new Line();
line.Stroke = SystemColors.WindowFrameBrush;
line.X1 = currentPoint.X;
line.Y1 = currentPoint.Y;
line.X2 = e.GetPosition(this).X;
line.Y2 = e.GetPosition(this).Y;
currentPoint = e.GetPosition(this);
paintSurface.Children.Add(line);
}
}
你能告诉我什么是缺失或如何重写它,以便它可以开始工作,帮助我吗?
没错。这正是我所做的。谢谢。 –
如何更新点击捕获来解决菜单造成的偏移? – Benjin
而不是在GetPosition中传递对窗口的引用,而是传递对Canvas的引用,而不是将coords与其相关联。 – Andy