示例#1
0
        /// <summary>
        /// Gets if drop position is directly over element
        /// </summary>
        /// <param name="dropPosition">Drop position</param>
        /// <param name="element">element to check whether or not the drop position is directly over or not</param>
        /// <param name="relativeToElement">element to which the drop position is related</param>
        /// <returns>drop position is directly over element or not</returns>
        public static bool DirectlyOverElement(this Point dropPosition, UIElement element, UIElement relativeToElement)
        {
            if (element == null)
            {
                return(false);
            }

            var relativeItemPosition = element.TranslatePoint(new Point(0, 0), relativeToElement);
            var relativeDropPosition = new Point(dropPosition.X - relativeItemPosition.X, dropPosition.Y - relativeItemPosition.Y);

            return(VisualTreeExtensions.GetVisibleDescendantBounds(element).Contains(relativeDropPosition));
        }
示例#2
0
        // Helper to generate the image - I grabbed this off Google
        // somewhere. -- Chris Bordeman [email protected]
        private static BitmapSource CaptureScreen(Visual target, FlowDirection flowDirection)
        {
            if (target == null)
            {
                return(null);
            }

            var bounds     = VisualTreeHelper.GetDescendantBounds(target);
            var cropBounds = VisualTreeExtensions.GetVisibleDescendantBounds(target);

            var dpiScale  = VisualTreeHelper.GetDpi(target);
            var dpiX      = dpiScale.PixelsPerInchX;
            var dpiY      = dpiScale.PixelsPerInchY;
            var dpiBounds = DpiHelper.LogicalRectToDevice(cropBounds, dpiScale.DpiScaleX, dpiScale.DpiScaleY);

            var pixelWidth  = (int)Math.Ceiling(dpiBounds.Width);
            var pixelHeight = (int)Math.Ceiling(dpiBounds.Height);

            if (pixelWidth < 0 || pixelHeight < 0)
            {
                return(null);
            }

            var rtb = new RenderTargetBitmap(pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Pbgra32);

            var dv = new DrawingVisual();

            using (var ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(target);

                // vb.ViewportUnits = BrushMappingMode.Absolute;
                // vb.Viewport = bounds;

                if (flowDirection == FlowDirection.RightToLeft)
                {
                    var transformGroup = new TransformGroup();
                    transformGroup.Children.Add(new ScaleTransform(-1, 1));
                    transformGroup.Children.Add(new TranslateTransform(bounds.Size.Width, 0));
                    ctx.PushTransform(transformGroup);
                }

                ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }

            rtb.Render(dv);

            return(rtb);
        }