0

I was trying to set the Clip on a Visual. The path is supposed to be a rounded rectangle with different corner radii. The current API for setting the CornerRadius on a RoundedRectangleGeometry allows setting a uniform radius on all the four corners

auto roundedRectangle = compositor.CreateRoundedRectangleGeometry();
roundedRectangle.CornerRadius(Float2(width, height));
clip.Geometry(roundedRectangle);
visual.Clip(clip);

But I'm trying to align with the CSS property of background-radius which permits setting different corner-radius for all the 4 corners:

border-radius: 10px 100px 20px 30px/ 30px 20px 10px 40px;

leading to something with all four different elliptical corners as

Different corner-radii

So in essence, I'm seeking a way to emulate this for a Visual's Clip property, allowing me to achieve a similar effect as the CSS property.

P.S. This is a continuing effort from this question. This is something I've already tried but applying the same logic of offsetting the clipping RoundedRectangle didn't work in this case.The answer to that question won't suffice for this requirement because the rounded corner radii were the same for 2 corners (in fact all 4 corners) in that case and the suggested solution would not be adequate for all different elliptical corners. Offsetting the clipping RoundedRectangle won't work here because I'd need 4 different RoundedRectangles and then somehow position them over the Visual but the Clip property allows only one CompositionGeometricClip with one Geometry.

I'm using the C++/WinRT language projection.

Zoso
  • 3,273
  • 1
  • 16
  • 27

1 Answers1

1

Uwp app hasn’t provided such api to do this. We suggest you could implement this requirement through an image whose source is a picture of specified shape in uwp.

In addition, you could use the WinUI3 api named RectangleClip Class to do this, which has TopLeftRadius, TopRightRadius, BottemLeftRadius, BottemRightRadius properties, so that you could set different corner-radius for 4 corners. To use this api, you need to create a WinUI 3 app. Please refer to the following code.

Xaml code:

<Grid x:Name="host1" Background="red">
     
</Grid>

Code behind:

       var cor = Window.Current.Compositor;
        RectangleClip rectclip = cor.CreateRectangleClip();
        rectclip.Left = 40;
        rectclip.Top = 20;
        rectclip.Right = 300;
        rectclip.Bottom = 150;
        rectclip.TopLeftRadius = new System.Numerics.Vector2(5);
        rectclip.TopRightRadius = new System.Numerics.Vector2(30);
        rectclip.BottomLeftRadius = new System.Numerics.Vector2(10);
        rectclip.BottomRightRadius = new System.Numerics.Vector2(60);
        Visual visual = ElementCompositionPreview.GetElementVisual(host1);
        visual.Clip = rectclip;
dear_vv
  • 2,350
  • 1
  • 4
  • 13
  • Unfortunately, we are yet to upgrade to WinUI3 and would need some time to migrate to that which is why the UWP compatibility is still required. Would you be able to elaborate more on the Image Source solution, if possible? – Zoso May 25 '21 at 10:19
  • My meaning is that you could create a picture of the desired shape(4 different corner radius), then set it to the source of an image control. – dear_vv May 26 '21 at 06:44
  • Ah, that won't be possible because it's not that we have a single shape that would be fixed. The corner radius can be set to anything (programmatically/by the user) and we couldn't possibly have Images to cater to all possible radius combinations. – Zoso May 26 '21 at 08:36
  • How about `Border` control? The `CornerRadius` property of border control could set different corner-radius for 4 corners. Then you could place control inside it, it will achieve a clip effect. – dear_vv May 27 '21 at 08:48
  • It seems that the [`cornerRadius`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.cornerradius?view=winrt-20348) can only support *circular* radii on all the four corners. I'm looking for elliptical radii. I might be wrong. Basically [`TopLeft`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.cornerradius.topleft?view=winrt-20348) is a `double` rather than a `Float2` – Zoso May 27 '21 at 08:53
  • It seems that circular radii would actually work for me but unfortunately, it seems that a `Border` can only wrap around a `UIElement` and not a `Visual` as per the documentation of the [`Child`](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.border.child?view=winrt-20348#Windows_UI_Xaml_Controls_Border_Child) property. As mentioned in my question, I'm looking for something around a `Visual`. Creating a `UIElement` around a `Visual` isn't an option because there are many child `Visual`s that this needs to be applied to and creating `UIElement`s to host all would be a problem – Zoso May 27 '21 at 09:21
  • 1
    We are consulting other engineers about your issue, there might be some delay. – dear_vv May 28 '21 at 08:57