programusy

Untitled

Sep 15th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 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 ShapeDrawingApp
  8. {
  9. public partial class MainWindow : Window
  10. {
  11. private enum DrawMode
  12. {
  13. None,
  14. Ellipse,
  15. Rectangle,
  16. Circle,
  17. Line
  18. }
  19.  
  20. private DrawMode currentDrawMode = DrawMode.None;
  21. private Point startPoint;
  22. private Shape currentShape;
  23.  
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28.  
  29. private void SetCurrentDrawMode(DrawMode mode)
  30. {
  31. currentDrawMode = mode;
  32. currentShape = null;
  33. }
  34.  
  35. private void btnEllipse_Click(object sender, RoutedEventArgs e)
  36. {
  37. SetCurrentDrawMode(DrawMode.Ellipse);
  38. }
  39.  
  40. private void btnRectangle_Click(object sender, RoutedEventArgs e)
  41. {
  42. SetCurrentDrawMode(DrawMode.Rectangle);
  43. }
  44.  
  45. private void btnCircle_Click(object sender, RoutedEventArgs e)
  46. {
  47. SetCurrentDrawMode(DrawMode.Circle);
  48. }
  49.  
  50. private void btnLine_Click(object sender, RoutedEventArgs e)
  51. {
  52. SetCurrentDrawMode(DrawMode.Line);
  53. }
  54.  
  55. private void drawingCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  56. {
  57. if (currentDrawMode != DrawMode.None)
  58. {
  59. startPoint = e.GetPosition(drawingCanvas);
  60. currentShape = CreateShape();
  61. drawingCanvas.Children.Add(currentShape);
  62. }
  63. }
  64.  
  65. private void drawingCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  66. {
  67. if (currentShape != null)
  68. {
  69. currentShape = null;
  70. }
  71. }
  72.  
  73. private void drawingCanvas_MouseMove(object sender, MouseEventArgs e)
  74. {
  75. if (currentShape != null && e.LeftButton == MouseButtonState.Pressed)
  76. {
  77. Point currentPoint = e.GetPosition(drawingCanvas);
  78. UpdateShape(currentShape, startPoint, currentPoint);
  79. }
  80. }
  81.  
  82. private Shape CreateShape()
  83. {
  84. Shape shape = null;
  85. switch (currentDrawMode)
  86. {
  87. case DrawMode.Ellipse:
  88. shape = new Ellipse();
  89. break;
  90.  
  91. case DrawMode.Rectangle:
  92. shape = new Rectangle();
  93. break;
  94.  
  95. case DrawMode.Circle:
  96. shape = new Ellipse();
  97. break;
  98.  
  99. case DrawMode.Line:
  100. shape = new Line();
  101. break;
  102. }
  103.  
  104. shape.Stroke = Brushes.Black;
  105. shape.StrokeThickness = 2;
  106. return shape;
  107. }
  108.  
  109. private void UpdateShape(Shape shape, Point startPoint, Point endPoint)
  110. {
  111. if (shape == null)
  112. return;
  113.  
  114. if (currentDrawMode == DrawMode.Line)
  115. {
  116. Line line = (Line)shape;
  117. line.X1 = startPoint.X;
  118. line.Y1 = startPoint.Y;
  119. line.X2 = endPoint.X;
  120. line.Y2 = endPoint.Y;
  121. }
  122. else
  123. {
  124. double left = Math.Min(startPoint.X, endPoint.X);
  125. double top = Math.Min(startPoint.Y, endPoint.Y);
  126. double width = Math.Abs(startPoint.X - endPoint.X);
  127. double height = Math.Abs(startPoint.Y - endPoint.Y);
  128.  
  129. Canvas.SetLeft(shape, left);
  130. Canvas.SetTop(shape, top);
  131.  
  132. if (currentDrawMode == DrawMode.Circle)
  133. {
  134. Ellipse ellipse = (Ellipse)shape;
  135. ellipse.Width = width;
  136. ellipse.Height = height;
  137. }
  138. else
  139. {
  140. shape.Width = width;
  141. shape.Height = height;
  142. }
  143. }
  144. }
  145. }
  146. }
  147.  
Add Comment
Please, Sign In to add comment