Exemplo n.º 1
0
        public static System.Drawing.Rectangle GetScreenBoundingRectangle(UIElement target)
        {
            PresentationSource source = PresentationSource.FromVisual(target);

            if (source == null)
            {
                throw new InvalidOperationException("The specified UiElement is not connected to a rendering Visual Tree.");
            }

            Matrix transform;

            try
            {
                System.Windows.Media.GeneralTransform gt =
                    target.TransformToAncestor(source.RootVisual);
                System.Windows.Media.Transform t = gt as System.Windows.Media.Transform;
                if (t != null)
                {
                    transform = t.Value;
                }
                else
                {
                    throw new System.ApplicationException("//TODO: Handle GeneralTransform Case - introduced by Transforms Breaking Change");
                }
            }
            catch (InvalidOperationException)
            {
                throw new InvalidOperationException("The specified UiElement is not connected to a rendering Visual Tree.");
            }
            Rect targetRect = new Rect(new Point(), target.RenderSize);

            targetRect.Transform(transform);

            Point rootOffset = targetRect.TopLeft;

            System.Windows.Media.CompositionTarget vm = source.CompositionTarget;
            rootOffset = vm.TransformToDevice.Transform(rootOffset);//vm.DeviceUnitsFromMeasureUnits(rootOffset);
            System.Drawing.Point topLeft = new System.Drawing.Point((int)rootOffset.X, (int)rootOffset.Y);
            User32.ClientToScreen(((System.Windows.Interop.HwndSource)source).Handle, ref topLeft);

            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(topLeft, new System.Drawing.Size(Convert.ToInt32(Monitor.ConvertLogicalToScreen(Dimension.Width, target.RenderSize.Width)), Convert.ToInt32(Monitor.ConvertLogicalToScreen(Dimension.Height, target.RenderSize.Height))));

            return(rect);
        }
Exemplo n.º 2
0
        void EnsureTransformMatrices()
        {
            if (this.compositionTarget == null)
            {
                var source = PresentationSource.FromVisual(this);

                if (source != null)
                {
                    this.compositionTarget = source.CompositionTarget;
                    this.xformFromDevice = this.compositionTarget.TransformFromDevice;
                    this.xformToDevice = this.compositionTarget.TransformToDevice;
                }
            }

        }