Advertisement
programusy

Untitled

Sep 15th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace ShapeDrawingApp
  6. {
  7. public partial class Form1 : Form
  8. {
  9. private enum ShapeType
  10. {
  11. Ellipse,
  12. RoundedRectangle,
  13. Rectangle
  14. }
  15.  
  16. private ShapeType currentShapeType = ShapeType.Ellipse;
  17. private bool isDrawing = false;
  18. private Point startPoint;
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. private void btnDrawShape_Click(object sender, EventArgs e)
  26. {
  27. isDrawing = true;
  28. startPoint = new Point(0, 0);
  29. }
  30.  
  31. private void drawingPanel_MouseDown(object sender, MouseEventArgs e)
  32. {
  33. if (isDrawing)
  34. {
  35. startPoint = e.Location;
  36. }
  37. }
  38.  
  39. private void drawingPanel_MouseUp(object sender, MouseEventArgs e)
  40. {
  41. if (isDrawing)
  42. {
  43. isDrawing = false;
  44. Point endPoint = e.Location;
  45.  
  46. int width = Math.Abs(endPoint.X - startPoint.X);
  47. int height = Math.Abs(endPoint.Y - startPoint.Y);
  48.  
  49. Graphics graphics = drawingPanel.CreateGraphics();
  50. Pen pen = new Pen(Color.Black, 2);
  51.  
  52. switch (currentShapeType)
  53. {
  54. case ShapeType.Ellipse:
  55. graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, width, height);
  56. break;
  57.  
  58. case ShapeType.RoundedRectangle:
  59. int cornerRadius = 20; // You can adjust this value
  60. Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, width, height);
  61. graphics.DrawRoundedRectangle(pen, rect, cornerRadius);
  62. break;
  63.  
  64. case ShapeType.Rectangle:
  65. graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, width, height);
  66. break;
  67. }
  68.  
  69. graphics.Dispose();
  70. }
  71. }
  72.  
  73. private void radioEllipse_CheckedChanged(object sender, EventArgs e)
  74. {
  75. currentShapeType = ShapeType.Ellipse;
  76. }
  77.  
  78. private void radioRoundedRectangle_CheckedChanged(object sender, EventArgs e)
  79. {
  80. currentShapeType = ShapeType.RoundedRectangle;
  81. }
  82.  
  83. private void radioRectangle_CheckedChanged(object sender, EventArgs e)
  84. {
  85. currentShapeType = ShapeType.Rectangle;
  86. }
  87. }
  88.  
  89. // Extension method for drawing rounded rectangles
  90. public static class GraphicsExtensions
  91. {
  92. public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, Rectangle rectangle, int cornerRadius)
  93. {
  94. int diameter = cornerRadius * 2;
  95. Size size = new Size(diameter, diameter);
  96. Rectangle arc = new Rectangle(rectangle.Location, size);
  97. graphics.DrawArc(pen, arc, 180, 90);
  98. arc.X = rectangle.Right - diameter;
  99. graphics.DrawArc(pen, arc, 270, 90);
  100. arc.Y = rectangle.Bottom - diameter;
  101. graphics.DrawArc(pen, arc, 0, 90);
  102. arc.X = rectangle.Left;
  103. graphics.DrawArc(pen, arc, 90, 90);
  104. graphics.DrawLine(pen, arc.X + cornerRadius, rectangle.Top, arc.X + rectangle.Width - cornerRadius, rectangle.Top);
  105. graphics.DrawLine(pen, arc.X + cornerRadius, rectangle.Bottom, arc.X + rectangle.Width - cornerRadius, rectangle.Bottom);
  106. graphics.DrawLine(pen, rectangle.Left, arc.Y + cornerRadius, rectangle.Left, arc.Y + rectangle.Height - cornerRadius);
  107. graphics.DrawLine(pen, rectangle.Right, arc.Y + cornerRadius, rectangle.Right, arc.Y + rectangle.Height - cornerRadius);
  108. }
  109. }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement