2013-04-24 86 views
1

我目前正试图在Visual Studio WPF窗体中制作一个使用鼠标绘制线条的绘图。 目前它正在绘制相同的旧线,并继续绘制它,但我想每次按下鼠标左键时画一条新线。这里的MainWindows.xaml.cs代码是什么样子:如何在C中使用鼠标绘制线条#

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

    } 
} 

}

而这里的MainWindow.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> 

什么我做错了吗?

回答

2

在我看来,您正在不断添加到PathSegment中,而不需要在开始新行时清除它(通过单击鼠标左键)。因此,我想试试这个:

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

编辑:...如果你想让它保留了线,他们不一定连接,你需要为每行一个单独的路径,因为每个路径代表一个,连接形状。

要做到这一点,你就需要修改你的代码是这样的:

  • 从XAML
  • 删除路径对于每一个点击鼠标左键,创建一个新的路径,并将它添加到网格(myGrid.Children.Add(mypath中)
  • 从鼠标移动新增点到当前的活动路径
+0

这工作,但如何将我可以画多行,而不删除最后的? – 2013-04-24 16:09:18

+0

@User_ T请参阅我的编辑。如果我的回答对您有用,请考虑将其作为答案或将其标记为答案。这就是你如何从社区成员获得免费帮助。谢谢。 – Kohanz 2013-04-25 15:00:53

相关问题