Esempio n. 1
0
        public void Start()
        {
            var     win     = App.Current.MainWindow as Designer;
            Ellipse ellipse = new Ellipse();
            var     started = false;

            var MapCanvas = win.MapCanvas;

            MapCanvas.CaptureMouse();

            ellipse.Fill            = win.backgroundColorPanel.Background;
            ellipse.Stroke          = win.foregroundColorPanel.Background;
            ellipse.StrokeThickness = 1;

            MapCanvas.Children.Add(ellipse);

            MapCanvas.PreviewMouseLeftButtonUp += (sender, args) =>
            {
                if (finished)
                {
                    return;
                }
                MapCanvas.ReleaseMouseCapture();
                //poly.Points.RemoveAt(poly.Points.Count  );
                var grd = new Viewbox();
                MapCanvas.Children.Remove(ellipse);
                grd.Child = (ellipse);

                var miny = Canvas.GetTop(ellipse);
                var minx = Canvas.GetLeft(ellipse);

                Canvas.SetTop(grd, miny);
                Canvas.SetLeft(grd, minx);

                ellipse.Margin              = new Thickness(0);
                ellipse.VerticalAlignment   = VerticalAlignment.Stretch;
                ellipse.HorizontalAlignment = HorizontalAlignment.Stretch;
                MapCanvas.Children.Add(grd);
                ShapeHandler.SelectedImage = grd;

                ShapeHandler.InitImageHandlers(ref grd);
                finished = true;
                Dispose();
            };

            Point startPoint = new Point();

            MapCanvas.PreviewMouseLeftButtonDown += (sender, args) =>
            {
                if (finished)
                {
                    return;
                }
                var pos = args.GetPosition(MapCanvas);
                Canvas.SetTop(ellipse, pos.Y);
                Canvas.SetLeft(ellipse, pos.X);
                startPoint = pos;
                started    = true;
            };

            MapCanvas.PreviewMouseMove += (sender, args) =>
            {
                if (finished || !started)
                {
                    return;
                }

                var pos = args.GetPosition(MapCanvas);
                var x   = Math.Min(pos.X, startPoint.X);
                var y   = Math.Min(pos.Y, startPoint.Y);

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

                ellipse.Width  = w;
                ellipse.Height = h;

                Canvas.SetLeft(ellipse, x);
                Canvas.SetTop(ellipse, y);
            };
        }
Esempio n. 2
0
        public void Start()
        {
            var     win  = App.Current.MainWindow as Designer;
            Polygon poly = new Polygon();

            var MapCanvas = win.MapCanvas;

            MapCanvas.CaptureMouse();

            poly.FillRule        = FillRule.Nonzero;
            poly.Fill            = win.backgroundColorPanel.Background;
            poly.Stroke          = win.foregroundColorPanel.Background;
            poly.StrokeThickness = 1;

            MapCanvas.Children.Add(poly);

            MapCanvas.PreviewMouseRightButtonDown += (sender, args) =>
            {
                if (finished)
                {
                    return;
                }
                MapCanvas.ReleaseMouseCapture();
                //poly.Points.RemoveAt(poly.Points.Count  );
                var grd = new Viewbox();
                MapCanvas.Children.Remove(poly);
                grd.Child = (poly);

                var miny = poly.Points.Min(_ => _.Y);
                var minx = poly.Points.Min(_ => _.X);

                Canvas.SetTop(grd, miny);
                Canvas.SetLeft(grd, minx);

                PointCollection pc = new PointCollection();
                for (int index = 0; index < poly.Points.Count; index++)
                {
                    var point = poly.Points[index];
                    point.Y = point.Y - miny;
                    point.X = point.X - minx;
                    pc.Add(point);
                }
                poly.Points = pc;

                poly.Margin              = new Thickness(0);
                poly.VerticalAlignment   = VerticalAlignment.Stretch;
                poly.HorizontalAlignment = HorizontalAlignment.Stretch;
                MapCanvas.Children.Add(grd);
                ShapeHandler.SelectedImage = grd;

                ShapeHandler.InitImageHandlers(ref grd);
                finished = true;
                Dispose();
            };

            MapCanvas.PreviewMouseLeftButtonDown += (sender, args) =>
            {
                if (finished)
                {
                    return;
                }
                var pos = args.GetPosition(MapCanvas);
                if (poly.Points.Count == 0)
                {
                    poly.Points.Add(pos);
                    poly.Points.Add(pos);
                }
                else
                {
                    poly.Points[poly.Points.Count - 1] = pos;
                    poly.Points.Add(pos);
                }
            };

            MapCanvas.PreviewMouseMove += (sender, args) =>
            {
                if (finished)
                {
                    return;
                }
                var pos = args.GetPosition(MapCanvas);
                if (poly.Points.Count != 0)
                {
                    poly.Points[poly.Points.Count - 1] = pos;
                }
            };
        }