Ejemplo n.º 1
0
        public static Visual Attach(UIElement element, float radius, float opacity, CompositionClip clip = null)
        {
            var elementVisual = ElementCompositionPreview.GetElementVisual(element);

            var shadow = elementVisual.Compositor.CreateDropShadow();

            shadow.BlurRadius = radius;
            shadow.Opacity    = opacity;
            shadow.Color      = Colors.Black;

            var visual = elementVisual.Compositor.CreateSpriteVisual();

            visual.Shadow = shadow;
            visual.Size   = new Vector2(0, 0);
            visual.Offset = new Vector3(0, 0, 0);
            visual.Clip   = clip;

            switch (element)
            {
            case Image image:
                shadow.Mask = image.GetAlphaMask();
                break;

            case Shape shape:
                shadow.Mask = shape.GetAlphaMask();
                break;

            case TextBlock textBlock:
                shadow.Mask = textBlock.GetAlphaMask();
                break;
            }

            ElementCompositionPreview.SetElementChildVisual(element, visual);
            return(visual);
        }
Ejemplo n.º 2
0
        IEnumerable <XObject> GetCompositionClipContents(CompositionClip obj)
        {
            foreach (var item in GetCompositionObjectContents(obj))
            {
                yield return(item);
            }

            yield return(FromVector2DefaultZero(nameof(obj.CenterPoint), obj.CenterPoint));

            yield return(FromVector2DefaultOne(nameof(obj.Scale), obj.Scale));
        }
Ejemplo n.º 3
0
        CompositionClip GetCompositionClip(CompositionClip obj)
        {
            switch (obj.Type)
            {
            case CompositionObjectType.InsetClip:
                return(GetInsetClip((InsetClip)obj));

            case CompositionObjectType.CompositionGeometricClip:
                return(GetCompositionGeometricClip((CompositionGeometricClip)obj));

            default:
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 4
0
        public static void ClipVisual(this FrameworkElement frameworkElement, Geometry geometry)
        {
            // UIElement.Clip only support rectangle geometry to be used for clipping area sizing.
            // If the used Build is 17763 or higher, we use Composition's APIs (CompositionGeometricClip) to allow Clip complex geometries.
#if UWP_18362
            var compositor = Window.Current.Compositor;
            var visual     = ElementCompositionPreview.GetElementVisual(frameworkElement);

            CompositionClip compositionClip = null;

            if (geometry is EllipseGeometry ellipseGeometry)
            {
                var compositionEllipseGeometry = compositor.CreateEllipseGeometry();

                compositionEllipseGeometry.Center = new WVector2((float)ellipseGeometry.Center.X, (float)ellipseGeometry.Center.Y);
                compositionEllipseGeometry.Radius = new WVector2((float)ellipseGeometry.RadiusX, (float)ellipseGeometry.RadiusY);

                compositionClip = compositor.CreateGeometricClip(compositionEllipseGeometry);
            }
            else if (geometry is LineGeometry lineGeometry)
            {
                var compositionLineGeometry = compositor.CreateLineGeometry();

                compositionLineGeometry.Start = new WVector2((float)lineGeometry.StartPoint.X, (float)lineGeometry.StartPoint.Y);
                compositionLineGeometry.End   = new WVector2((float)lineGeometry.EndPoint.X, (float)lineGeometry.EndPoint.Y);

                compositionClip = compositor.CreateGeometricClip(compositionLineGeometry);
            }
            else if (geometry is RectangleGeometry rectangleGeometry)
            {
                var compositionRectangleGeometry = compositor.CreateRectangleGeometry();

                compositionRectangleGeometry.Offset = new WVector2((float)rectangleGeometry.Rect.X, (float)rectangleGeometry.Rect.Y);
                compositionRectangleGeometry.Size   = new WVector2((float)rectangleGeometry.Rect.Width, (float)rectangleGeometry.Rect.Height);

                compositionClip = compositor.CreateGeometricClip(compositionRectangleGeometry);
            }

            if (visual.Clip != compositionClip)
            {
                visual.Clip = compositionClip;
            }
#endif
        }
Ejemplo n.º 5
0
        public static void ClipVisual(this FrameworkElement frameworkElement, Geometry geometry)
        {
            var compositor = ElementCompositionPreview.GetElementVisual(frameworkElement).Compositor;
            var visual     = ElementCompositionPreview.GetElementVisual(frameworkElement);

            CompositionClip compositionClip = null;

            if (geometry is EllipseGeometry ellipseGeometry)
            {
                var compositionEllipseGeometry = compositor.CreateEllipseGeometry();

                compositionEllipseGeometry.Center = new WVector2((float)ellipseGeometry.Center.X, (float)ellipseGeometry.Center.Y);
                compositionEllipseGeometry.Radius = new WVector2((float)ellipseGeometry.RadiusX, (float)ellipseGeometry.RadiusY);

                compositionClip = compositor.CreateGeometricClip(compositionEllipseGeometry);
            }
            else if (geometry is LineGeometry lineGeometry)
            {
                var compositionLineGeometry = compositor.CreateLineGeometry();

                compositionLineGeometry.Start = new WVector2((float)lineGeometry.StartPoint.X, (float)lineGeometry.StartPoint.Y);
                compositionLineGeometry.End   = new WVector2((float)lineGeometry.EndPoint.X, (float)lineGeometry.EndPoint.Y);

                compositionClip = compositor.CreateGeometricClip(compositionLineGeometry);
            }
            else if (geometry is RectangleGeometry rectangleGeometry)
            {
                var compositionRectangleGeometry = compositor.CreateRectangleGeometry();

                compositionRectangleGeometry.Offset = new WVector2((float)rectangleGeometry.Rect.X, (float)rectangleGeometry.Rect.Y);
                compositionRectangleGeometry.Size   = new WVector2((float)rectangleGeometry.Rect.Width, (float)rectangleGeometry.Rect.Height);

                compositionClip = compositor.CreateGeometricClip(compositionRectangleGeometry);
            }

            if (visual.Clip != compositionClip)
            {
                visual.Clip = compositionClip;
            }
        }
 public static void SetClip(this UIElement element, CompositionClip value)
 {
     element.GetVisual().Clip = value;
 }
 public static void SetClip(this UIElement element, CompositionClip value)
 {
     element.GetVisual().Clip = value;
 }
 public static void SetClip(this DependencyObject element, CompositionClip value)
 {
     (element as UIElement).GetVisual().Clip = value;
 }