C# WPF-使用鼠标事件在画布上绘制
我在画布上处理鼠标事件时遇到问题。我想用鼠标在上面画画,我已经想出了这些事件处理程序,但当我开始画画时,它们什么都不做C# WPF-使用鼠标事件在画布上绘制,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我在画布上处理鼠标事件时遇到问题。我想用鼠标在上面画画,我已经想出了这些事件处理程序,但当我开始画画时,它们什么都不做 private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ButtonState == MouseButtonState.Pressed) currentPoint = e.GetPosition(this);
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);
}
}
您能告诉我缺少什么或如何重写它以使其开始工作吗?我敢打赌,您的画布不会接收鼠标事件,因为它的“背景”属性设置为“透明” 这对我来说很好
<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" Height="350" Width="525">
<Canvas Name="paintSurface" MouseDown="Canvas_MouseDown_1" MouseMove="Canvas_MouseMove_1" >
<Canvas.Background>
<SolidColorBrush Color="White" Opacity="0"/>
</Canvas.Background>
</Canvas>
</Window>
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
Point currentPoint = new Point();
public MainWindow()
{
InitializeComponent();
}
private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
}
private void Canvas_MouseMove_1(object sender, System.Windows.Input.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);
}
}
}
}
使用制度;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Input;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
公共部分类主窗口:窗口
{
点currentPoint=新点();
公共主窗口()
{
初始化组件();
}
私有void Canvas_MouseDown_1(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{
如果(e.ButtonState==MouseButtonState.Pressed)
currentPoint=e.GetPosition(此);
}
私有void Canvas_MouseMove_1(对象发送方,System.Windows.Input.MouseEventArgs e)
{
如果(e.LeftButton==鼠标按钮状态。按下)
{
行=新行();
line.Stroke=SystemColors.WindowFrameBrush;
line.X1=currentPoint.X;
line.Y1=当前点.Y;
line.X2=e.GetPosition(this).X;
line.Y2=e.GetPosition(this).Y;
currentPoint=e.GetPosition(此);
漆面。儿童。添加(线);
}
}
}
}
使用Line时,粗线(Line.StrokeThickness=20)如下所示:
所以我尝试了多段线,效果很好
简单使用InkCanvas
<InkCanvas x:Name="InkCanvas" x:FieldModifier="public" Background="Transparent" Opacity="1" EditingMode="GestureOnly" ForceCursor="True" Cursor="Pen" >
<InkCanvas.DefaultDrawingAttributes>
<DrawingAttributes Color="White" Width="7" Height="7" />
</InkCanvas.DefaultDrawingAttributes>
</InkCanvas>
是的。我就是这么做的。谢谢。我如何更新点击捕获来解释菜单造成的偏移?不要在GetPosition中传递对窗口的引用,而是传递对画布的引用,这样坐标就相对于画布了。+1是Andy的评论。如果您添加边距,您将看到difference@Andy我是否在所有GetPosition函数中都通过了PaintSurface(您有三个)?我仍然会得到奇怪的结果:无论我在哪里单击,它都会以画布的边缘为起点,画一条线到单击的点,然后继续正常的面。这可以正常工作,但在鼠标悬停后会显示形状
Canvas.MouseMove += (sender, args) =>
{
if (args.LeftButton == MouseButtonState.Pressed)
{
Polyline polyLine;
if (PathModeCanvas.Children.Count == 0)
{
polyLine = new Polyline();
polyLine.Stroke = new SolidColorBrush(Colors.AliceBlue);
polyLine.StrokeThickness = 10;
Canvas.Children.Add(polyLine);
}
polyLine = (Polyline)Canvas.Children[0];
Point currentPoint = args.GetPosition(Canvas);
polyLine.Points.Add(currentPoint);
}
};
<InkCanvas x:Name="InkCanvas" x:FieldModifier="public" Background="Transparent" Opacity="1" EditingMode="GestureOnly" ForceCursor="True" Cursor="Pen" >
<InkCanvas.DefaultDrawingAttributes>
<DrawingAttributes Color="White" Width="7" Height="7" />
</InkCanvas.DefaultDrawingAttributes>
</InkCanvas>