C# 如何在C语言中用鼠标画线#

C# 如何在C语言中用鼠标画线#,c#,wpf,mouse,lines,C#,Wpf,Mouse,Lines,我目前正试图在VisualStudioWPF表单中创建一个pragram,它在绘画中使用类似鼠标的方式绘制线条。 目前,它正在绘制相同的旧线,并继续绘制它,但我希望每次按下鼠标左键时都绘制一条新线。以下是MainWindows.xaml.cs代码及其外观: namespace DrawingLines { public partial class MainWindow : Window { public MainWindow() { InitializeComp

我目前正试图在VisualStudioWPF表单中创建一个pragram,它在绘画中使用类似鼠标的方式绘制线条。 目前,它正在绘制相同的旧线,并继续绘制它,但我希望每次按下鼠标左键时都绘制一条新线。以下是MainWindows.xaml.cs代码及其外观:

namespace DrawingLines
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private PathFigure _pathFigure = new PathFigure();
    PathFigureCollection _pathCollection = new PathFigureCollection();
    PathSegmentCollection _segments = new PathSegmentCollection();
    private PathGeometry _pathGeometry = new PathGeometry();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _pathFigure.Segments = _segments;
        _pathCollection.Add(_pathFigure);
        _pathGeometry.Figures = _pathCollection;
        myPath.Data = _pathGeometry;
    }

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            LineSegment segment = new LineSegment();
            segment.Point = e.GetPosition(this);
            _pathFigure.Segments.Add(segment);
        }

    }

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _pathFigure.StartPoint = e.GetPosition(this);
    }

    //private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    //{

    //}

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.Close();

    }
}
}

下面是主窗口.xmal代码:

<Window x:Class="DrawingLines.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Joonistamine" Height="350" Width="525" Loaded="Window_Loaded" MouseMove="Window_MouseMove" MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseRightButtonDown="Window_MouseRightButtonDown">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="53*" />
        <RowDefinition Height="258*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="124*" />
        <ColumnDefinition Width="379*" />
    </Grid.ColumnDefinitions>
    <Path Stroke="Black" StrokeThickness="1" Name="myPath" Grid.ColumnSpan="2" Grid.RowSpan="2" />
    <Button Content="Exit" Height="25" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="46" Click="button1_Click" BorderBrush="Red" Foreground="#FFFF1A1A" />
</Grid>


我做错了什么?

在我看来,您不断地添加到PathSegment,而在开始新行时(通过单击鼠标左键),却没有清除它。因此,我想尝试一下:

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _pathFigure.Segments.Clear();
    _pathFigure.StartPoint = e.GetPosition(this);
}
编辑:。。。如果您希望它保留线,并且它们不一定是连接的,那么您需要为每条线指定一条单独的路径,因为每条路径代表一个单独的、连接的形状

为此,您需要以以下方式修改代码:

  • 从XAML中删除路径
  • 对于每次鼠标左键单击,创建一个新路径并将其添加到网格(myGrid.Children.add(myPath))中
  • 将鼠标移动的新点添加到当前活动路径

这是有效的,但是如何删除多个行而不删除最后一行呢?@ USELT请参阅我的编辑。如果我的答案对你有用,请考虑将其投票或标记为答案。这是你从社区成员那里得到免费帮助的方式。谢谢。