Advertisement
programusy

Untitled

Sep 15th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 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 LineCircleRectangleDrawingApp
  8. {
  9. public partial class MainWindow : Window
  10. {
  11. private Line currentLine;
  12. private Ellipse currentCircle;
  13. private Rectangle currentRectangle;
  14. private Point startPoint;
  15. private bool isDrawingLine = false;
  16. private bool isDrawingCircle = false;
  17. private bool isDrawingRectangle = false;
  18.  
  19. public MainWindow()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void btnDrawLine_Click(object sender, RoutedEventArgs e)
  25. {
  26. if (currentCircle != null)
  27. {
  28. canvas.Children.Remove(currentCircle);
  29. currentCircle = null;
  30. }
  31.  
  32. if (currentRectangle != null)
  33. {
  34. canvas.Children.Remove(currentRectangle);
  35. currentRectangle = null;
  36. }
  37.  
  38. if (currentLine == null)
  39. {
  40. currentLine = new Line
  41. {
  42. Stroke = Brushes.Black,
  43. StrokeThickness = 2
  44. };
  45. canvas.Children.Add(currentLine);
  46. }
  47. isDrawingLine = true;
  48. isDrawingCircle = false;
  49. isDrawingRectangle = false;
  50. }
  51.  
  52. private void btnDrawCircle_Click(object sender, RoutedEventArgs e)
  53. {
  54. if (currentLine != null)
  55. {
  56. canvas.Children.Remove(currentLine);
  57. currentLine = null;
  58. }
  59.  
  60. if (currentRectangle != null)
  61. {
  62. canvas.Children.Remove(currentRectangle);
  63. currentRectangle = null;
  64. }
  65.  
  66. if (currentCircle == null)
  67. {
  68. currentCircle = new Ellipse
  69. {
  70. Stroke = Brushes.Black,
  71. StrokeThickness = 2
  72. };
  73. canvas.Children.Add(currentCircle);
  74. }
  75. isDrawingCircle = true;
  76. isDrawingLine = false;
  77. isDrawingRectangle = false;
  78. }
  79.  
  80. private void btnDrawRectangle_Click(object sender, RoutedEventArgs e)
  81. {
  82. if (currentLine != null)
  83. {
  84. canvas.Children.Remove(currentLine);
  85. currentLine = null;
  86. }
  87.  
  88. if (currentCircle != null)
  89. {
  90. canvas.Children.Remove(currentCircle);
  91. currentCircle = null;
  92. }
  93.  
  94. if (currentRectangle == null)
  95. {
  96. currentRectangle = new Rectangle
  97. {
  98. Stroke = Brushes.Black,
  99. StrokeThickness = 2,
  100. Fill = Brushes.Transparent
  101. };
  102. canvas.Children.Add(currentRectangle);
  103. }
  104. isDrawingRectangle = true;
  105. isDrawingLine = false;
  106. isDrawingCircle = false;
  107. }
  108.  
  109. private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  110. {
  111. startPoint = e.GetPosition(canvas);
  112.  
  113. if (isDrawingLine)
  114. {
  115. if (currentLine != null)
  116. {
  117. currentLine.X1 = startPoint.X;
  118. currentLine.Y1 = startPoint.Y;
  119. currentLine.X2 = startPoint.X;
  120. currentLine.Y2 = startPoint.Y;
  121. }
  122. }
  123. else if (isDrawingCircle)
  124. {
  125. if (currentCircle != null)
  126. {
  127. currentCircle.Width = 0;
  128. currentCircle.Height = 0;
  129. Canvas.SetLeft(currentCircle, startPoint.X);
  130. Canvas.SetTop(currentCircle, startPoint.Y);
  131. }
  132. }
  133. else if (isDrawingRectangle)
  134. {
  135. if (currentRectangle != null)
  136. {
  137. currentRectangle.Width = 0;
  138. currentRectangle.Height = 0;
  139. Canvas.SetLeft(currentRectangle, startPoint.X);
  140. Canvas.SetTop(currentRectangle, startPoint.Y);
  141. }
  142. }
  143. }
  144.  
  145. private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  146. {
  147. if (isDrawingLine)
  148. {
  149. if (currentLine != null)
  150. {
  151. Point endPoint = e.GetPosition(canvas);
  152. currentLine.X2 = endPoint.X;
  153. currentLine.Y2 = endPoint.Y;
  154. }
  155. }
  156. else if (isDrawingCircle)
  157. {
  158. if (currentCircle != null)
  159. {
  160. Point endPoint = e.GetPosition(canvas);
  161. double radiusX = endPoint.X - startPoint.X;
  162. double radiusY = endPoint.Y - startPoint.Y;
  163. double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
  164.  
  165. currentCircle.Width = 2 * radius;
  166. currentCircle.Height = 2 * radius;
  167.  
  168. Canvas.SetLeft(currentCircle, startPoint.X - radius);
  169. Canvas.SetTop(currentCircle, startPoint.Y - radius);
  170. }
  171. }
  172. else if (isDrawingRectangle)
  173. {
  174. if (currentRectangle != null)
  175. {
  176. Point endPoint = e.GetPosition(canvas);
  177. double width = endPoint.X - startPoint.X;
  178. double height = endPoint.Y - startPoint.Y;
  179.  
  180. currentRectangle.Width = System.Math.Abs(width);
  181. currentRectangle.Height = System.Math.Abs(height);
  182.  
  183. if (width < 0)
  184. {
  185. Canvas.SetLeft(currentRectangle, endPoint.X);
  186. }
  187. else
  188. {
  189. Canvas.SetLeft(currentRectangle, startPoint.X);
  190. }
  191.  
  192. if (height < 0)
  193. {
  194. Canvas.SetTop(currentRectangle, endPoint.Y);
  195. }
  196. else
  197. {
  198. Canvas.SetTop(currentRectangle, startPoint.Y);
  199. }
  200. }
  201. }
  202. }
  203.  
  204. private void canvas_MouseMove(object sender, MouseEventArgs e)
  205. {
  206. if (isDrawingLine)
  207. {
  208. if (currentLine != null && e.LeftButton == MouseButtonState.Pressed)
  209. {
  210. Point currentPoint = e.GetPosition(canvas);
  211. currentLine.X2 = currentPoint.X;
  212. currentLine.Y2 = currentPoint.Y;
  213. }
  214. }
  215. else if (isDrawingCircle)
  216. {
  217. if (currentCircle != null && e.LeftButton == MouseButtonState.Pressed)
  218. {
  219. Point currentPoint = e.GetPosition(canvas);
  220. double radiusX = currentPoint.X - startPoint.X;
  221. double radiusY = currentPoint.Y - startPoint.Y;
  222. double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
  223.  
  224. currentCircle.Width = 2 * radius;
  225. currentCircle.Height = 2 * radius;
  226.  
  227. Canvas.SetLeft(currentCircle, startPoint.X - radius);
  228. Canvas.SetTop(currentCircle, startPoint.Y - radius);
  229. }
  230. }
  231. else if (isDrawingRectangle)
  232. {
  233. if (currentRectangle != null && e.LeftButton == MouseButtonState.Pressed)
  234. {
  235. Point currentPoint = e.GetPosition(canvas);
  236. double width = currentPoint.X - startPoint.X;
  237. double height = currentPoint.Y - startPoint.Y;
  238.  
  239. currentRectangle.Width = System.Math.Abs(width);
  240. currentRectangle.Height = System.Math.Abs(height);
  241.  
  242. if (width < 0)
  243. {
  244. Canvas.SetLeft(currentRectangle, currentPoint.X);
  245. }
  246. else
  247. {
  248. Canvas.SetLeft(currentRectangle, startPoint.X);
  249. }
  250.  
  251. if (height < 0)
  252. {
  253. Canvas.SetTop(currentRectangle, currentPoint.Y);
  254. }
  255. else
  256. {
  257. Canvas.SetTop(currentRectangle, startPoint.Y);
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement