Win10 1803版本下实现Composition Visual单侧圆角的方案问询
Win10 1803(17134版本)单侧圆角实现方案
针对Win10 1803版本不支持Compositor.CreateGeometricClip()接口、无法直接修改InsetClip/ShapeVisual尺寸的限制,可以通过「全圆角基础形状+局部裁剪+位置偏移」的思路实现需求,具体实现步骤如下:
1. 创建全圆角基础ShapeVisual
和现有实现逻辑一致,先生成四个角带统一圆角的ShapeVisual实例:
auto roundedRectGeometry = compositor.CreateRoundedRectangleGeometry(); // 创建圆角几何 roundedRectGeometry.Size(float2{targetWidth, targetHeight}); // targetWidth、targetHeight为目标控件的实际宽高 roundedRectGeometry.CornerRadius(float2{cornerRadius, cornerRadius}); // cornerRadius为需要设置的圆角值 auto spriteShape = compositor.CreateSpriteShape(roundedRectGeometry); // 创建精灵形状 auto shapeVisual = compositor.CreateShapeVisual(); // 创建形状视觉对象 shapeVisual.Size(float2{targetWidth, targetHeight}); shapeVisual.Shapes().Append(spriteShape);
2. 用InsetClip裁剪不需要圆角的一侧
利用InsetClip原生支持的四边裁剪偏移属性,直接裁剪掉不需要圆角的一侧的圆角区域:
- 仅保留左侧圆角:设置右侧裁剪偏移等于圆角半径,裁掉右侧两个圆角
auto insetClip = compositor.CreateInsetClip(); insetClip.RightInset = cornerRadius; shapeVisual.Clip = insetClip; - 仅保留右侧圆角:设置左侧裁剪偏移等于圆角半径,裁掉左侧两个圆角
auto insetClip = compositor.CreateInsetClip(); insetClip.LeftInset = cornerRadius; shapeVisual.Clip = insetClip; - 仅保留顶部/底部圆角:同理设置
BottomInset/TopInset为圆角半径即可 - 仅保留单个角:同时设置两个相邻方向的裁剪偏移为圆角半径,比如仅保留左上角圆角就设置
RightInset = cornerRadius和BottomInset = cornerRadius
3. 偏移形状对齐目标控件
裁剪后ShapeVisual的可视区域会比目标尺寸少一个圆角半径的长度,向需要保留圆角的一侧偏移对应半径距离,即可对齐目标控件边缘:
- 仅保留左侧圆角:向左偏移圆角半径距离
shapeVisual.Offset(float2(-cornerRadius, 0)); - 仅保留右侧圆角:向右偏移圆角半径距离
shapeVisual.Offset(float2(cornerRadius, 0)); - 单角场景:向对应角的方向偏移两个方向的圆角半径即可
该方案所有调用的API均为Win10 1803原生支持接口,无额外版本依赖,可正常实现单侧圆角需求。
内容的提问来源于stack exchange,提问作者Zoso
相关产品推荐
相关产品推荐

