Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace LineCircleRectangleDrawingApp
- {
- public partial class MainWindow : Window
- {
- private Line currentLine;
- private Ellipse currentCircle;
- private Rectangle currentRectangle;
- private Point startPoint;
- private bool isDrawingLine = false;
- private bool isDrawingCircle = false;
- private bool isDrawingRectangle = false;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void btnDrawLine_Click(object sender, RoutedEventArgs e)
- {
- if (currentCircle != null)
- {
- canvas.Children.Remove(currentCircle);
- currentCircle = null;
- }
- if (currentRectangle != null)
- {
- canvas.Children.Remove(currentRectangle);
- currentRectangle = null;
- }
- if (currentLine == null)
- {
- currentLine = new Line
- {
- Stroke = Brushes.Black,
- StrokeThickness = 2
- };
- canvas.Children.Add(currentLine);
- }
- isDrawingLine = true;
- isDrawingCircle = false;
- isDrawingRectangle = false;
- }
- private void btnDrawCircle_Click(object sender, RoutedEventArgs e)
- {
- if (currentLine != null)
- {
- canvas.Children.Remove(currentLine);
- currentLine = null;
- }
- if (currentRectangle != null)
- {
- canvas.Children.Remove(currentRectangle);
- currentRectangle = null;
- }
- if (currentCircle == null)
- {
- currentCircle = new Ellipse
- {
- Stroke = Brushes.Black,
- StrokeThickness = 2
- };
- canvas.Children.Add(currentCircle);
- }
- isDrawingCircle = true;
- isDrawingLine = false;
- isDrawingRectangle = false;
- }
- private void btnDrawRectangle_Click(object sender, RoutedEventArgs e)
- {
- if (currentLine != null)
- {
- canvas.Children.Remove(currentLine);
- currentLine = null;
- }
- if (currentCircle != null)
- {
- canvas.Children.Remove(currentCircle);
- currentCircle = null;
- }
- if (currentRectangle == null)
- {
- currentRectangle = new Rectangle
- {
- Stroke = Brushes.Black,
- StrokeThickness = 2,
- Fill = Brushes.Transparent
- };
- canvas.Children.Add(currentRectangle);
- }
- isDrawingRectangle = true;
- isDrawingLine = false;
- isDrawingCircle = false;
- }
- private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- startPoint = e.GetPosition(canvas);
- if (isDrawingLine)
- {
- if (currentLine != null)
- {
- currentLine.X1 = startPoint.X;
- currentLine.Y1 = startPoint.Y;
- currentLine.X2 = startPoint.X;
- currentLine.Y2 = startPoint.Y;
- }
- }
- else if (isDrawingCircle)
- {
- if (currentCircle != null)
- {
- currentCircle.Width = 0;
- currentCircle.Height = 0;
- Canvas.SetLeft(currentCircle, startPoint.X);
- Canvas.SetTop(currentCircle, startPoint.Y);
- }
- }
- else if (isDrawingRectangle)
- {
- if (currentRectangle != null)
- {
- currentRectangle.Width = 0;
- currentRectangle.Height = 0;
- Canvas.SetLeft(currentRectangle, startPoint.X);
- Canvas.SetTop(currentRectangle, startPoint.Y);
- }
- }
- }
- private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (isDrawingLine)
- {
- if (currentLine != null)
- {
- Point endPoint = e.GetPosition(canvas);
- currentLine.X2 = endPoint.X;
- currentLine.Y2 = endPoint.Y;
- }
- }
- else if (isDrawingCircle)
- {
- if (currentCircle != null)
- {
- Point endPoint = e.GetPosition(canvas);
- double radiusX = endPoint.X - startPoint.X;
- double radiusY = endPoint.Y - startPoint.Y;
- double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
- currentCircle.Width = 2 * radius;
- currentCircle.Height = 2 * radius;
- Canvas.SetLeft(currentCircle, startPoint.X - radius);
- Canvas.SetTop(currentCircle, startPoint.Y - radius);
- }
- }
- else if (isDrawingRectangle)
- {
- if (currentRectangle != null)
- {
- Point endPoint = e.GetPosition(canvas);
- double width = endPoint.X - startPoint.X;
- double height = endPoint.Y - startPoint.Y;
- currentRectangle.Width = System.Math.Abs(width);
- currentRectangle.Height = System.Math.Abs(height);
- if (width < 0)
- {
- Canvas.SetLeft(currentRectangle, endPoint.X);
- }
- else
- {
- Canvas.SetLeft(currentRectangle, startPoint.X);
- }
- if (height < 0)
- {
- Canvas.SetTop(currentRectangle, endPoint.Y);
- }
- else
- {
- Canvas.SetTop(currentRectangle, startPoint.Y);
- }
- }
- }
- }
- private void canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (isDrawingLine)
- {
- if (currentLine != null && e.LeftButton == MouseButtonState.Pressed)
- {
- Point currentPoint = e.GetPosition(canvas);
- currentLine.X2 = currentPoint.X;
- currentLine.Y2 = currentPoint.Y;
- }
- }
- else if (isDrawingCircle)
- {
- if (currentCircle != null && e.LeftButton == MouseButtonState.Pressed)
- {
- Point currentPoint = e.GetPosition(canvas);
- double radiusX = currentPoint.X - startPoint.X;
- double radiusY = currentPoint.Y - startPoint.Y;
- double radius = System.Math.Sqrt(radiusX * radiusX + radiusY * radiusY);
- currentCircle.Width = 2 * radius;
- currentCircle.Height = 2 * radius;
- Canvas.SetLeft(currentCircle, startPoint.X - radius);
- Canvas.SetTop(currentCircle, startPoint.Y - radius);
- }
- }
- else if (isDrawingRectangle)
- {
- if (currentRectangle != null && e.LeftButton == MouseButtonState.Pressed)
- {
- Point currentPoint = e.GetPosition(canvas);
- double width = currentPoint.X - startPoint.X;
- double height = currentPoint.Y - startPoint.Y;
- currentRectangle.Width = System.Math.Abs(width);
- currentRectangle.Height = System.Math.Abs(height);
- if (width < 0)
- {
- Canvas.SetLeft(currentRectangle, currentPoint.X);
- }
- else
- {
- Canvas.SetLeft(currentRectangle, startPoint.X);
- }
- if (height < 0)
- {
- Canvas.SetTop(currentRectangle, currentPoint.Y);
- }
- else
- {
- Canvas.SetTop(currentRectangle, startPoint.Y);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement