2013-04-24 71 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