Advertisement
programusy

Untitled

Sep 15th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using System.Windows.Media;
  5. using System.Windows.Shapes;
  6.  
  7. namespace LineDrawingApp
  8. {
  9. public partial class MainWindow : Window
  10. {
  11. private Line currentLine;
  12. private Point startPoint;
  13. private bool isDrawing = false;
  14.  
  15. public MainWindow()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void btnDrawLine_Click(object sender, RoutedEventArgs e)
  21. {
  22. if (currentLine == null)
  23. {
  24. currentLine = new Line
  25. {
  26. Stroke = Brushes.Black,
  27. StrokeThickness = 2
  28. };
  29. canvas.Children.Add(currentLine);
  30. }
  31. }
  32.  
  33. private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  34. {
  35. if (currentLine != null)
  36. {
  37. startPoint = e.GetPosition(canvas);
  38. currentLine.X1 = startPoint.X;
  39. currentLine.Y1 = startPoint.Y;
  40. currentLine.X2 = startPoint.X;
  41. currentLine.Y2 = startPoint.Y;
  42. isDrawing = true;
  43. }
  44. }
  45.  
  46. private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  47. {
  48. if (currentLine != null && isDrawing)
  49. {
  50. Point endPoint = e.GetPosition(canvas);
  51. currentLine.X2 = endPoint.X;
  52. currentLine.Y2 = endPoint.Y;
  53. currentLine = null;
  54. isDrawing = false;
  55. }
  56. }
  57.  
  58. private void canvas_MouseMove(object sender, MouseEventArgs e)
  59. {
  60. if (currentLine != null && isDrawing)
  61. {
  62. Point currentPoint = e.GetPosition(canvas);
  63. currentLine.X2 = currentPoint.X;
  64. currentLine.Y2 = currentPoint.Y;
  65. }
  66. }
  67. }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement