WPF 关于圆角的制作

news/2024/7/16 6:52:29
原文: WPF 关于圆角的制作

1、使用Boder(一般情况):

设置CornerRadius属性

 <Border x:Name="border" CornerRadius="20">

...

</Border>

 

2、创建ClippingBorder类:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
 
namespace Shgbit.Controls {
    /// <Remarks>
    ///     As a side effect ClippingBorder will surpress any databinding or animation of 
    ///         its childs UIElement.Clip property until the child is removed from ClippingBorder
    /// </Remarks>
    public class ClippingBorder : Border {
        protected override void OnRender(DrawingContext dc) {
            OnApplyChildClip();
            base.OnRender(dc);
        }
 
        public override UIElement Child {
            get {
                return base.Child;
            }
            set {
                if (this.Child != value) {
                    if (this.Child != null) {
                        // Restore original clipping
                        this.Child.SetValue(UIElement.ClipProperty, _oldClip);
                    }
 
                    if (value != null) {
                        _oldClip = value.ReadLocalValue(UIElement.ClipProperty);
                    } else {
                        // If we dont set it to null we could leak a Geometry object
                        _oldClip = null;
                    }
 
                    base.Child = value;
                }
            }
        }
 
        protected virtual void OnApplyChildClip() {
            UIElement child = this.Child;
            if (child != null) {
                _clipRect.RadiusX = _clipRect.RadiusY = Math.Max(0.0, this.CornerRadius.TopLeft - (this.BorderThickness.Left * 0.5));
                Rect rect = new Rect(this.RenderSize);
                rect.Height -= (this.BorderThickness.Top + this.BorderThickness.Bottom);
                rect.Width -= (this.BorderThickness.Left + this.BorderThickness.Right);
                _clipRect.Rect = rect;
                child.Clip = _clipRect;
            }
        }
 
        public void Update() { OnApplyChildClip(); }
 
        private RectangleGeometry _clipRect = new RectangleGeometry();
        private object _oldClip;
    }
}

用法:
需应用xmlns:control="你的命名空间"

<control:ClippingBorder CornerRadius="20">
    <StackPanel Background="Yellow">
        <Label>Hello World</Label>
    </StackPanel>
</control:ClippingBorder>

 

3、使用Clip(通过路径):

<Border Width="300" Height="100">
    <Border.Clip>
        <PathGeometry>
            <PathGeometry.Figures>
                <PathFigure StartPoint="0,0" IsClosed="True">
                    <LineSegment Point="300,0" />
                    <LineSegment Point="300,80" />
                    <ArcSegment Point="280,100" Size="20,20" SweepDirection="Clockwise"/>
                    <LineSegment Point="0,100" />
                </PathFigure>
            </PathGeometry.Figures>
        </PathGeometry>
    </Border.Clip>
    <StackPanel Background="Yellow">
        <Label>Hello World</Label>
    </StackPanel>
</Border>

 


http://www.niftyadmin.cn/n/1214765.html

相关文章

举例说明层次分析的三大原则_LabVIEW面向对象编程_初窥门槛(2)_设计原则(SOLID)...

该系列一共是9篇文章&#xff0c;本文是该系列的第二篇&#xff0c;是讲述通用的面向对象编程设计原则&#xff08;SOLID&#xff09;&#xff0c;LabVIEW作为一门具备面向对象编程范式的图形化编程语言当然也要遵循上述的通用设计原则&#xff0c;当然除了接口隔离原则无法直接…

ubuntu12.04图形界面与命令行界面切换

对于12.04的ubuntu桌面系统&#xff0c;如果想在开机的时候直接进入字符界面&#xff0c;那可以&#xff1a; 编辑文件 /etc/init/lightdm.conf&#xff0c;在第12行附近&#xff0c;原句“ and runlevel [!06]” 改为“ and runlevel [!026]”即可&#xff0c; 之后如果想切换…

android 点击效果图,Android MaterialDesign之水波点击效果的几种实现方法

添加一个自定义的布局类 MaterialLayout.classpublic class MaterialLayout extends RelativeLayout {private static final int DEFAULT_RADIUS 10;private static final int DEFAULT_FRAME_RATE 10;private static final int DEFAULT_DURATION 200;private static final i…

accdb原有的数据怎么清除_VBA与数据库,自动化完成你大量数据处理工作的利器...

大家好&#xff0c;VBA是我在各个平台的主要推广的一大课题&#xff0c;VBA全称是Visual Basic for Applications&#xff0c;是实现个人小型数据自动化处理的最为可靠的工具。我们在平时的工作中&#xff0c;数据处理一直是工作的主要内容之一&#xff0c;如何利用VBA&#xf…

ec200s 封装_英飞凌OptiMOS?源极底置功率MOSFET系列新添PQFN封装的40 V装置

当代的电源系统设计需要高功率密度等级和精巧的外型尺寸&#xff0c;以期得到最高的系统级性能。英飞凌科技股份公司近日通过专注于强化元器件产品达到系统创新&#xff0c;来应对这一挑战。继2 月份推出 25 V 装置后&#xff0c;英飞凌又推出了 OptiMOS™ 40 V 低电压功率 MOS…

10月23日云栖精选夜读 | 2018 中国开源年度报告发布,阿里系独占鳌头

近日&#xff0c;开源社发布了《2018 中国开源年度报告》&#xff0c;以调研和数据报告的方式展示了中国开源的全貌。其中阿里系独占鳌头&#xff0c;活跃度前5的开源产品阿里系占了4个。而在这4个开源产品中&#xff0c;只有PouchContainer是非前端产品。 热点热议 2018 中国开…

android9 pixel编译刷机,为pixel手机编译aosp源码

前言目的&#xff1a;修改调试标志打印native动态注册地址改机脱壳(fart)编译android 7源码。分两个步骤&#xff1a;下载android源码到硬盘&#xff0c;大概有100G左右使用docker进行编译所有操作都在docker中进行一、构建docker环境参考资料&#xff1a;https://mirrors.tuna…

企业上云之机房和BGP带宽篇(一)

本篇是本系列的第二篇&#xff0c;第一篇讲了服务器的种种方面&#xff0c;而与服务器相关性最大的&#xff0c;就是网络。网络把世界上所有网络设备、服务器、个人电脑连接起来&#xff0c;成为了互联网(Internet)。 传统的做法是自购服务器&#xff0c;然后选择一个运营商机房…