Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace ShapeDrawingApp
- {
- public partial class Form1 : Form
- {
- private enum ShapeType
- {
- Ellipse,
- RoundedRectangle,
- Rectangle
- }
- private ShapeType currentShapeType = ShapeType.Ellipse;
- private bool isDrawing = false;
- private Point startPoint;
- public Form1()
- {
- InitializeComponent();
- }
- private void btnDrawShape_Click(object sender, EventArgs e)
- {
- isDrawing = true;
- startPoint = new Point(0, 0);
- }
- private void drawingPanel_MouseDown(object sender, MouseEventArgs e)
- {
- if (isDrawing)
- {
- startPoint = e.Location;
- }
- }
- private void drawingPanel_MouseUp(object sender, MouseEventArgs e)
- {
- if (isDrawing)
- {
- isDrawing = false;
- Point endPoint = e.Location;
- int width = Math.Abs(endPoint.X - startPoint.X);
- int height = Math.Abs(endPoint.Y - startPoint.Y);
- Graphics graphics = drawingPanel.CreateGraphics();
- Pen pen = new Pen(Color.Black, 2);
- switch (currentShapeType)
- {
- case ShapeType.Ellipse:
- graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, width, height);
- break;
- case ShapeType.RoundedRectangle:
- int cornerRadius = 20; // You can adjust this value
- Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, width, height);
- graphics.DrawRoundedRectangle(pen, rect, cornerRadius);
- break;
- case ShapeType.Rectangle:
- graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, width, height);
- break;
- }
- graphics.Dispose();
- }
- }
- private void radioEllipse_CheckedChanged(object sender, EventArgs e)
- {
- currentShapeType = ShapeType.Ellipse;
- }
- private void radioRoundedRectangle_CheckedChanged(object sender, EventArgs e)
- {
- currentShapeType = ShapeType.RoundedRectangle;
- }
- private void radioRectangle_CheckedChanged(object sender, EventArgs e)
- {
- currentShapeType = ShapeType.Rectangle;
- }
- }
- // Extension method for drawing rounded rectangles
- public static class GraphicsExtensions
- {
- public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, Rectangle rectangle, int cornerRadius)
- {
- int diameter = cornerRadius * 2;
- Size size = new Size(diameter, diameter);
- Rectangle arc = new Rectangle(rectangle.Location, size);
- graphics.DrawArc(pen, arc, 180, 90);
- arc.X = rectangle.Right - diameter;
- graphics.DrawArc(pen, arc, 270, 90);
- arc.Y = rectangle.Bottom - diameter;
- graphics.DrawArc(pen, arc, 0, 90);
- arc.X = rectangle.Left;
- graphics.DrawArc(pen, arc, 90, 90);
- graphics.DrawLine(pen, arc.X + cornerRadius, rectangle.Top, arc.X + rectangle.Width - cornerRadius, rectangle.Top);
- graphics.DrawLine(pen, arc.X + cornerRadius, rectangle.Bottom, arc.X + rectangle.Width - cornerRadius, rectangle.Bottom);
- graphics.DrawLine(pen, rectangle.Left, arc.Y + cornerRadius, rectangle.Left, arc.Y + rectangle.Height - cornerRadius);
- graphics.DrawLine(pen, rectangle.Right, arc.Y + cornerRadius, rectangle.Right, arc.Y + rectangle.Height - cornerRadius);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement