wpf-绘图-路径-基础


Path最重要的属性是Data,Data的数据类型是Geometry(是抽象类,要用派生类的标签),赋值方法有两种,一种是标签式标准语法,第二种是路径标记语法。

基础知识

Geometry的子类有:
LineGeometry:直线几何图形
RectangleGeometry:矩形几何图形
EllipseGeometry
PathGeometry
StreamGeometry:PathGeometry的轻量级替代品,不支持Binding、动画等
CombinedGeometry:由多个基本几何图形联合在一起,形成的单一几何图形
GeometryGroup:这个是一组,不是单一图形

前面的Line/Rectangle/Ellipse类都是可以独立存在的对象,而LineGeometry这些只能用于结合成其他几何图形,不能独立存在。当在Blend中选一组独立的几何图形并在菜单里执行组合路径的命令时,本质是把原本独立的Line/Rectangle/Ellipse变成了*Geometry对象,然后组合成一个新的复杂图形。

在这里插入图片描述
绘制<PathFigure.Segments>时,这些线段都没有起点,因为起点就是前一个线段的终点。第一个线段的起点是PathFigure的StartPoint。

Path实例:标签定义

在这里插入图片描述

<UserControl x:Class="pxy.PathLabelBasic"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:pxy"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="400">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="160"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="160"/>
            <RowDefinition Height="240"/>
        </Grid.RowDefinitions>
        <!--直线-->
        <Path Stroke="CornflowerBlue" StrokeThickness="2" Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,20" EndPoint="140,140"/>
            </Path.Data>
        </Path>
        <!--矩形-->
        <Path Stroke="Orange" Fill="Yellow" Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="20,20,120,120" RadiusX="10" RadiusY="10"/>
            </Path.Data>
        </Path>
        <!--椭圆-->
        <Path Stroke="Green" Fill="LawnGreen" Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="80,80" RadiusX="60" RadiusY="40"/>
            </Path.Data>
        </Path>
        <!--自定义路径-->
        <Path Stroke="Yellow" Fill="Orange" Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures> <!--Figures是PathGeometry的默认内容属性,可省略-->
                        <PathFigure StartPoint="25,140" IsClosed="True">
                            <PathFigure.Segments> <!--Segments也是默认内容属性,也可以省略-->
                                <LineSegment Point="20,40"/>
                                <LineSegment Point="40,110"/>
                                <ArcSegment Point="110,150" Size="80,80"/> <!--连接终点-->
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
    </Grid>
</UserControl>

说明:
ArcSegment的Point属性用来指明圆弧连接的终点;圆弧截取自椭圆,Size指定的是横轴半径和纵轴半径;SweepDirection指的是圆弧是按顺时针方向还是逆时针方向。如果椭圆上两点不对称,那两点间的圆弧可分为大弧和小弧,IsLargeArc可以用来指明是否用大弧连接。RotationAngle指明圆弧母椭圆的旋转角度。

举例(用到了后面的路径标记语法)

    <Grid>
        <Path Stroke="Black" Fill="Bisque" Data="M 10,10 A 180,100 30 0 1 150,150"/>
    </Grid>

说明:M 10,10 指从头开始,A表示开始画圆弧,(180,100)是椭圆的横轴半径和纵轴半径,30指椭圆的旋转角度(椭圆中心与原点连线,与y正半轴的夹角),0指小弧,1指圆弧是顺时针,(150,150)是线段终点。
在这里插入图片描述

<Path Stroke="Black" Fill="Bisque" Data="M 10,10 A 180,100 30 1 1 150,150"/>

在这里插入图片描述

贝塞尔曲线BezierSegment

起点:前一个线段的终点/PathFigure的StartPoint
终点:Point3属性
两个控制点:Point1和Point2
在这里插入图片描述
三次方贝塞尔曲线就是从起点走向Point1,再走向Point2方向,最后到达终点的平滑曲线。

二次方贝塞尔曲线QuadraticBezierSegment

控制点是Point1,终点Point2
在这里插入图片描述

<QuadraticBezierSegment Point1="150,-100" Point2="160,200"/>

GeometryGroup

在< Path><Path.Data>下可以放< GeometryGroup>,其下可以放几个< PathGeometry>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值