WPF中如何设置自定义控件(二)

前一篇文章中简要讲解了圆角按钮、圆形按钮的使用,以及在windows.resource和app.resource中设置圆角或圆形按钮的样式。

这篇主要讲解Polygon(多边形)、Ellipse(椭圆)、Path(路径)这三个内容。

Polygon

我们先看一下的源码:

namespace System.Windows.Shapes
{
    public sealed class Polygon : Shape
    {
        public static readonly DependencyProperty PointsProperty = DependencyProperty.Register("Points", typeof(PointCollection), typeof(Polygon), new FrameworkPropertyMetadata((object)new FreezableDefaultValueFactory((Freezable)PointCollection.get_Empty()), FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register("FillRule", typeof(FillRule), typeof(Polygon), (PropertyMetadata)new FrameworkPropertyMetadata(FillRule.EvenOdd, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender), (ValidateValueCallback)ValidateEnums.IsFillRuleValid);

        private Geometry _polygonGeometry;

        public PointCollection Points
        {
            get
            {
                return (PointCollection)GetValue(PointsProperty);
            }
            set
            {
                SetValue(PointsProperty, value);
            }
        }

        public FillRule FillRule
        {
            get
            {
                return (FillRule)GetValue(FillRuleProperty);
            }
            set
            {
                SetValue(FillRuleProperty, value);
            }
        }

        protected override Geometry DefiningGeometry => _polygonGeometry;

        internal override void CacheDefiningGeometry()
        {
            PointCollection points = Points;
            PathFigure pathFigure = new PathFigure();
            if (points == null)
            {
                _polygonGeometry = Geometry.Empty;
                return;
            }

            if (points.Count > 0)
            {
                pathFigure.StartPoint = points[0];
                if (points.Count > 1)
                {
                    Point[] array = new Point[points.Count - 1];
                    for (int i = 1; i < points.Count; i++)
                    {
                        array[i - 1] = points[i];
                    }

                    pathFigure.Segments.Add(new PolyLineSegment(array, isStroked: true));
                }

                pathFigure.IsClosed = true;
            }

            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures.Add(pathFigure);
            pathGeometry.FillRule = FillRule;
            _polygonGeometry = pathGeometry;
        }
    }

}

从源码的信息可以看到Polygon是继承自Shape的类,可用的属性只有PointsProperty、FillRuleProperty两个属性;PointsProperty是PointCollection的Point集合,而Point实质就是一个二维坐标集合,因此在Polygon的使用中Points的中的数据必须是2N个。用法如下:

<Polygon Points="100,400 200,370 180,470" Fill="#4EB1B6" /><!--多边形-->

效果图:

Ellipse 

源码如下:

namespace System.Windows.Shapes
{
    public sealed class Ellipse : Shape
    {
        private Rect _rect = Rect.Empty;

        public override Geometry RenderedGeometry => DefiningGeometry;

        public override Transform GeometryTransform => Transform.Identity;

        protected override Geometry DefiningGeometry
        {
            get
            {
                if (_rect.IsEmpty)
                {
                    return Geometry.Empty;
                }

                return new EllipseGeometry(_rect);
            }
        }

        internal override int EffectiveValuesInitialSize => 13;

        static Ellipse()
        {
            Shape.StretchProperty.OverrideMetadata(typeof(Ellipse), new FrameworkPropertyMetadata(Stretch.Fill));
        }

        protected override Size MeasureOverride(Size constraint)
        {
            if (base.Stretch == Stretch.UniformToFill)
            {
                double width = constraint.Width;
                double height = constraint.Height;
                if (double.IsInfinity(width) && double.IsInfinity(height))
                {
                    return GetNaturalSize();
                }

                width = ((!double.IsInfinity(width) && !double.IsInfinity(height)) ? Math.Max(width, height) : Math.Min(width, height));
                return new Size(width, width);
            }

            return GetNaturalSize();
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double strokeThickness = GetStrokeThickness();
            double num = strokeThickness / 2.0;
            _rect = new Rect(num, num, Math.Max(0.0, finalSize.Width - strokeThickness), Math.Max(0.0, finalSize.Height - strokeThickness));
            switch (base.Stretch)
            {
                case Stretch.None:
                    {
                        double num4 = (_rect.Width = (_rect.Height = 0.0));
                        break;
                    }
                case Stretch.Uniform:
                    if (_rect.Width > _rect.Height)
   

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/511217.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3学习记录(三)--- 组合式API之生命周期和模板引用

一、生命周期 1、简介 ​ 生命周期&#xff0c;指的是一个 Vue 实例从创建到销毁的完整阶段&#xff0c;强调的是一个时间段。 ​ 生命周期钩子函数&#xff0c;指的是 Vue 实例提供的内置函数&#xff0c;函数的参数为一个回调函数。这些钩子函数会在实例生命周期的某些固定…

Spring(22) Spring中的9种设计模式

目录 一、简单工厂模式&#xff08;Simple Factory&#xff09;二、工厂方法模式&#xff08;Factory Method&#xff09;三、单例模式&#xff08;Singleton&#xff09;四、适配器模式&#xff08;Adapter&#xff09;五、代理模式&#xff08;Proxy&#xff09;七、观察者模…

【ARM Trace32(劳特巴赫) 高级篇 21 -- SystemTrace ITM 使用介绍】

文章目录 SystemTrace ITMSystemTrace ITM 常用命令Trace Data AnalysisSystemTrace ITM CoreSight ITM (Instrumentation Trace Macrocell) provides the following information: Address, data value and instruction address for selected data cyclesInterrupt event info…

就业班 2401--3.4 Linux Day10--软件管理

一、软件管理 导语&#xff1a; 安装软件 rpm yum 源码安装 ​ 卸载软件 rpm介绍 rpm软件包名称: 软件名称 版本号(主版本、次版本、修订号) 操作系统 -----90%的规律 #有依赖关系,不能自动解决依赖关系。 举例&#xff1a;openssh-6.6.1p1-31.el7.x86_64.rpm 数字前面的是名…

SkyWalking链路追踪上下文TraceContext的追踪身份traceId生成的实现原理剖析

结论先行 SkyWalking 通过字节码增强技术实现&#xff0c;结合依赖注入和控制反转思想&#xff0c;以SkyWalking方式将追踪身份traceId编织到链路追踪上下文TraceContext中。 是不是很有趣&#xff0c;很有意思&#xff01;&#xff01;&#xff01; 实现原理剖析 TraceConte…

Kubernetes基础(二十七)-nodePort/targetPort/port/containerPort/hostPort

1 nodePort/targetPort/port/containerPort 1.1 实现层级 1.2 配置方式 ########service########### apiVersion: v1 kind: Service metadata: labels: name: app1 name: app1 namespace: default spec: type: NodePort ports: - <strong>port: 8080 t…

xss.haozi:0x00

0x00没有什么过滤所以怎么写都没有关系有很多解 <script>alert(1)</script>

嵌入式驱动学习第二周——Linux内核打印

前言 这篇博客来聊一聊Linux内核打印。 嵌入式驱动学习专栏将详细记录博主学习驱动的详细过程&#xff0c;未来预计四个月将高强度更新本专栏&#xff0c;喜欢的可以关注本博主并订阅本专栏&#xff0c;一起讨论一起学习。现在关注就是老粉啦&#xff01; 目录 前言1. dmesg指令…

1.1_2 性能指标——速率、带宽、吞吐量

文章目录 1.1_2 性能指标——速率、带宽、吞吐量&#xff08;一&#xff09;速率&#xff08;二&#xff09;带宽&#xff08;三&#xff09;吞吐量 1.1_2 性能指标——速率、带宽、吞吐量 &#xff08;一&#xff09;速率 速率即数据率或称数据传输率或比特率。 速率就是“快…

python自动化管理和zabbix监控网络设备(zabbix部署监控网络设备以及验证部分)

目录 前言 一、Zabbix搭建 二、FW1 三、python脚本 四、core-sw1 五、core-sw2 六、DMZ-sw1 前言 详细配置视频解析访问&#xff1a;白帽小丑的个人空间-白帽小丑个人主页-哔哩哔哩视频 一、Zabbix搭建 sed -i s/SELINUXenforcing/SELINUXdisable/ /etc/selinux/config…

如何在MinIO系统中进行配置并结合内网穿透实现公网远程连接上传文件

文章目录 前言1. 创建Buckets和Access Keys2. Linux 安装Cpolar3. 创建连接MinIO服务公网地址4. 远程调用MinIO服务小结5. 固定连接TCP公网地址6. 固定地址连接测试 前言 MinIO是一款高性能、分布式的对象存储系统&#xff0c;它可以100%的运行在标准硬件上&#xff0c;即X86等…

后端传给前端的时间字段前端显示不正确

具体问题是什么呢&#xff0c;就比如我后段有一个字段是TimeStamp类型&#xff0c;从数据库中查出数据是下面的样式&#xff1a; 但是前端显示的是下面的格式&#xff1a; 这个的解决方法还是挺多的&#xff0c;那接下来具体来看看吧~ 第一种&#xff1a; 在application.prop…