예제 #1
0
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (line && e.LeftButton == MouseButtonState.Released)
            {
                currentPosition = new Point(0, 0);
                List <UIElement> copy = new List <UIElement>();
                copy.AddRange(HistoryElement);
                History.Add(copy);
                HistoryElement.Clear();
                UndoButton.IsEnabled = (UndoList.Count <= 0);
            }

            if (rectangle && e.LeftButton == MouseButtonState.Released)
            {
                HistoryElement.Add(_rectangle);
                _rectangle = new Rectangle();
                List <UIElement> copy = new List <UIElement>();
                copy.AddRange(HistoryElement);
                History.Add(copy);
                HistoryElement.Clear();
                UndoButton.IsEnabled = (UndoList.Count <= 0);
            }

            if (ellipse && e.LeftButton == MouseButtonState.Released)
            {
                HistoryElement.Add(_ellipse);
                _ellipse = new Ellipse();
                List <UIElement> copy = new List <UIElement>();
                copy.AddRange(HistoryElement);
                History.Add(copy);
                HistoryElement.Clear();
                UndoButton.IsEnabled = (UndoList.Count <= 0);
            }

            if (triangle && e.LeftButton == MouseButtonState.Released)
            {
                HistoryElement.Add(_triangle);
                _triangle = new Polygon();
                List <UIElement> copy = new List <UIElement>();
                copy.AddRange(HistoryElement);
                History.Add(copy);
                HistoryElement.Clear();
                UndoButton.IsEnabled = (UndoList.Count <= 0);
            }
        }
예제 #2
0
        private void Canvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (line && e.LeftButton == MouseButtonState.Pressed)
            {
                _line                    = new Line();
                _line.Stroke             = Color;
                _line.StrokeStartLineCap = PenLineCap.Round;
                _line.StrokeEndLineCap   = PenLineCap.Round;
                _line.StrokeThickness    = Thickness;
                _line.X1                 = currentPosition.X;
                _line.Y1                 = currentPosition.Y - 80;
                _line.X2                 = e.GetPosition(this).X;
                _line.Y2                 = e.GetPosition(this).Y - 80;
                currentPosition          = e.GetPosition(this);
                Canvas.Children.Add(_line);
                HistoryElement.Add(_line);
            }
            if (rectangle && e.LeftButton == MouseButtonState.Pressed)
            {
                _rectangle.Stroke          = Color;
                _rectangle.StrokeThickness = Thickness;
                if ((bool)fill)
                {
                    _rectangle.Fill = Color;
                }
                Canvas.SetLeft(_rectangle, currentPosition.X);
                Canvas.SetTop(_rectangle, currentPosition.Y);
                var pos = e.GetPosition(Canvas);

                var x = Math.Min(pos.X, currentPosition.X);
                var y = Math.Min(pos.Y, currentPosition.Y);

                var w = Math.Max(pos.X, currentPosition.X) - x;
                var h = Math.Max(pos.Y, currentPosition.Y) - y;

                _rectangle.Width  = w;
                _rectangle.Height = h;

                Canvas.SetLeft(_rectangle, x);
                Canvas.SetTop(_rectangle, y);
            }
            if (ellipse && e.LeftButton == MouseButtonState.Pressed)
            {
                _ellipse.Stroke          = Color;
                _ellipse.StrokeThickness = Thickness;
                if ((bool)fill)
                {
                    _ellipse.Fill = Color;
                }
                Canvas.SetLeft(_ellipse, currentPosition.X);
                Canvas.SetTop(_ellipse, currentPosition.Y);
                var pos = e.GetPosition(Canvas);

                var x = Math.Min(pos.X, currentPosition.X);
                var y = Math.Min(pos.Y, currentPosition.Y);

                var w = Math.Max(pos.X, currentPosition.X) - x;
                var h = Math.Max(pos.Y, currentPosition.Y) - y;

                _ellipse.Width  = w;
                _ellipse.Height = h;

                Canvas.SetLeft(_ellipse, x);
                Canvas.SetTop(_ellipse, y);
            }

            if (triangle && e.LeftButton == MouseButtonState.Pressed)
            {
                _triangle.Stroke          = Color;
                _triangle.StrokeThickness = Thickness;
                if ((bool)fill)
                {
                    _triangle.Fill = Color;
                }
                Canvas.SetLeft(_triangle, currentPosition.X);
                Canvas.SetTop(_triangle, currentPosition.Y);
                var pos = e.GetPosition(Canvas);

                Point p1 = new Point(0, pos.Y - currentPosition.Y);
                Point p2 = new Point(pos.X - currentPosition.X, pos.Y - currentPosition.Y);
                Point p3 = new Point((pos.X - currentPosition.X) / 2, 0);
                _triangle.Points[0] = p1;
                _triangle.Points[1] = p2;
                _triangle.Points[2] = p3;

                Canvas.SetLeft(_triangle, currentPosition.X);
                Canvas.SetTop(_triangle, currentPosition.Y);
            }
        }