Wpf draw ellipse at random location, magnify/highlight ellipse when selected, move ellipse

FredGrit發表於2024-03-27

Completely implemented via C# and without the intervation of xaml

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp18
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Canvas cvs;
        bool isMoving = false;
        Point currentPoint;
        Shape currentShape;
        int originalWidth = 50, originalHeight = 50;
        int originalStrokeThickness = 5;

        public MainWindow()
        {
            WindowState = WindowState.Maximized;
            InitializeComponent();
            cvs = new Canvas();
            cvs.MouseLeftButtonDown += Cvs_MouseLeftButtonDown;
            cvs.MouseLeftButtonUp += Cvs_MouseLeftButtonUp;
            cvs.MouseMove += Cvs_MouseMove;
            Loaded += delegate
            {
                DrawEllipseRandom();
            };
        }

        private void Cvs_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMoving)
            {
                var pt = e.GetPosition(cvs);
                Canvas.SetLeft(currentShape, Canvas.GetLeft(currentShape) + pt.X - currentPoint.X);
                Canvas.SetTop(currentShape, Canvas.GetTop(currentShape) + pt.Y - currentPoint.Y);
                currentPoint = pt;
            }
        }

        private void Cvs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isMoving)
            {
                isMoving = false;
                currentShape.Width = originalWidth;
                currentShape.Height = originalHeight;
                currentShape.StrokeThickness = originalStrokeThickness;
                currentShape.Fill = new SolidColorBrush(Colors.Red);
                cvs.ReleaseMouseCapture();
            }
        }

        private void Cvs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var shape = e.Source as Shape;
            if (shape != null)
            {
                isMoving = true;
                currentPoint = e.GetPosition(cvs);
                currentShape = shape;
                currentShape.Width = 2 * originalWidth;
                currentShape.Height = 2 * originalHeight;
                currentShape.StrokeThickness = 2 * originalStrokeThickness;
                currentShape.Fill = new SolidColorBrush(Colors.Yellow);
                cvs.CaptureMouse();
            }
        }

        private void DrawEllipseRandom()
        {
            double width = this.ActualWidth;
            double height = this.ActualHeight;

            Random rnd = new Random();
            for (int i = 0; i < 100; i++)
            {
                Ellipse elp = new Ellipse();
                elp.Stroke = new SolidColorBrush(Colors.Black);
                elp.Fill = new SolidColorBrush(Colors.Red);
                elp.StrokeThickness = originalStrokeThickness;
                elp.Width = originalWidth;
                elp.Height = originalHeight;
                cvs.Children.Add(elp);
                //avoid outbound 
                Canvas.SetLeft(elp, rnd.NextDouble()*(width-100));
                Canvas.SetTop(elp, rnd.NextDouble()*(height-100));
            }
            this.Content = cvs;
        }
    }
}

Original effects

Selected then magnify and highlight

Move

相關文章