Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace LineDrawingApp
- {
- public partial class MainWindow : Window
- {
- private Line currentLine;
- private Point startPoint;
- private bool isDrawing = false;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void btnDrawLine_Click(object sender, RoutedEventArgs e)
- {
- if (currentLine == null)
- {
- currentLine = new Line
- {
- Stroke = Brushes.Black,
- StrokeThickness = 2
- };
- canvas.Children.Add(currentLine);
- }
- }
- private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (currentLine != null)
- {
- startPoint = e.GetPosition(canvas);
- currentLine.X1 = startPoint.X;
- currentLine.Y1 = startPoint.Y;
- currentLine.X2 = startPoint.X;
- currentLine.Y2 = startPoint.Y;
- isDrawing = true;
- }
- }
- private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (currentLine != null && isDrawing)
- {
- Point endPoint = e.GetPosition(canvas);
- currentLine.X2 = endPoint.X;
- currentLine.Y2 = endPoint.Y;
- currentLine = null;
- isDrawing = false;
- }
- }
- private void canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (currentLine != null && isDrawing)
- {
- Point currentPoint = e.GetPosition(canvas);
- currentLine.X2 = currentPoint.X;
- currentLine.Y2 = currentPoint.Y;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement