Esempio n. 1
0
 // -------------------------------------------------        DefineRectangles
 private void DefineRectangles ()
 {
     int nW = ClientSize .Width;
     int nH = ClientSize .Height;
     rrNone = new ResizableRectangle (new Rectangle (nW / 5, nH / 5, nW / 5, nH / 8), Draw_Nonresizable); 
     Rectangle rc = new Rectangle (nW / 7, nH * 2 / 5, nW / 5, nH / 5);
     rrNS = new ResizableRectangle (rc, new Size (rc .Width, rc .Height / 3), new Size (rc .Width, rc .Height * 3),
                                    radius, halfstrip, Draw_NS); 
     rc = new Rectangle (nW / 6, nH * 11 / 15, nW / 4, nH / 5);
     rrWE = new ResizableRectangle (rc, new Size (rc .Width / 4, rc .Height), new Size (rc .Width * 4, rc .Height),
                                    radius, halfstrip, Draw_WE); 
     rrAny = new ResizableRectangle (new Rectangle (nW * 8 / 15, nH * 3 / 5, nW / 4, nH / 4),
                                     new Size (0, -12), new Size (500, 400), radius, halfstrip, Draw_Any);
 }
        protected override void OnPointerPressed(PointerRoutedEventArgs e)
        {
            var  point = e.GetCurrentPoint(RootCanvas).Position;
            Rect rectangle;
            var  transform = new CompositeTransform();

            foreach (var child in RootCanvas.Children)
            {
                if (child.GetType() != typeof(Rectangle))
                {
                    continue;
                }

                transform = child.RenderTransform as CompositeTransform;
                var rect = new Rect(transform.TranslateX, transform.TranslateY, (child as Rectangle).Width * transform.ScaleX, (child as Rectangle).Height * transform.ScaleY);
                if (rect.Contains(point))
                {
                    rectangle = rect;
                    break;
                }

                rectangle = new Rect();
            }

            RootCanvas.Children.Remove(RootCanvas.Children.Where(x => x.GetType() == typeof(ResizableRectangle)).FirstOrDefault());
            RootCanvas.Children.Remove(RootCanvas.Children.Where(x => x.GetType() == typeof(ResizableRectangle)).FirstOrDefault());

            if (rectangle == new Rect())
            {
                return;
            }

            //var resizableRect = new ResizableRectangle
            //{
            //    Height = rectangle.Height + 2,
            //    Width = rectangle.Width + 2,
            //    //Top = rectangle.Top,
            //    //Right = rectangle.Right,
            //    //Bottom = rectangle.Bottom,
            //    //Left = rectangle.Left
            //};

            //resizableRect.BorderBrush = new SolidColorBrush(Colors.Black);
            //resizableRect.BorderThickness = new Thickness(4);
            //resizableRect.BorderDashArray = new DoubleCollection() { 3, 2 };

            //resizableRect.PointerReleased += ResizableRect_PointerReleased;

            //Canvas.SetLeft(resizableRect, rectangle.X - 1);
            //Canvas.SetTop(resizableRect, rectangle.Y - 1);

            var resizableRectV2 = new ResizableRectangle
            {
                Top              = rectangle.Top,
                Right            = rectangle.Right,
                Bottom           = rectangle.Bottom,
                Left             = rectangle.Left,
                Width            = rectangle.Width,
                Height           = rectangle.Height,
                Stroke           = new SolidColorBrush(Colors.DarkBlue),
                StrokeThickness  = 2,
                BorderBrush      = new SolidColorBrush(Colors.White),
                ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY,
                CompositeMode    = ElementCompositeMode.SourceOver
            };

            resizableRectV2.ManipulationCompleted += delegate(object sender, ManipulationCompletedRoutedEventArgs args)
            {
                var matrix = resizableRectV2.CustomTransformMatrix;
            };

            //resizableRectV2.PointerReleased += delegate (object sender, PointerRoutedEventArgs args)
            //{
            //    var handler = sender as ResizableRectangle;
            //    var matrix = handler.CustomTransformMatrix;

            //    var annotation = RootCanvas.Children.Where(x => (x as Rectangle).Name == "annotation").FirstOrDefault();
            //    annotation.RenderTransform = new CompositeTransform
            //    {
            //        ScaleX = matrix.M11,
            //        ScaleY = matrix.M22,
            //        TranslateX = matrix.OffsetX,
            //        TranslateY = matrix.OffsetY
            //    };
            //};

            //Canvas.SetLeft(resizableRectV2, rectangle.X - 1);
            //Canvas.SetTop(resizableRectV2, rectangle.Y - 1); ;

            // RootCanvas.Children.Add(resizableRect);
            RootCanvas.Children.Add(resizableRectV2);

            base.OnPointerPressed(e);
        }