WPF 10天修炼 第九天 - 几何图形

几何图形

使用LineGeometry、RectangleGeometry、EllipseGeometry对象分别绘制直线、矩形、椭圆。

使用GeometryGroup可以绘制组合图形。

<Window x:Class="WPFDemo.GeometryDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFDemo"
        mc:Ignorable="d"
       WindowStartupLocation="CenterScreen"
        Title="GeometryDemo" Height="500" Width="800">
   <Canvas>
        <StackPanel Canvas.Top="10" >
            <TextBlock Text="绘制无相交组合图形"></TextBlock>
            <Path StrokeThickness="1" Fill="Yellow"  Stroke="Black" Margin="5" >
                <Path.Data  >
                    <!--使用GeometryGroup组合集合图形-->
                    <GeometryGroup >
                        <!--绘制直线-->
                        <LineGeometry StartPoint="10,20" EndPoint="100,20" />
                        <!--绘制矩形-->
                        <RectangleGeometry Rect="10,50,100,50" />
                        <!--绘制椭圆-->
                        <EllipseGeometry RadiusX="50" RadiusY="25" Center="60,160"/>
                    </GeometryGroup>
                </Path.Data>
            </Path>
        </StackPanel>
        <StackPanel Canvas.Top="10" Canvas.Left="150">
            <TextBlock Text="Nonzero方式填充图形"></TextBlock>
            <Path StrokeThickness="1" Fill="Yellow"  Stroke="Black" Margin="5">
                <Path.Data  >
                    <!--使用GeometryGroup组合集合图形-->
                    <GeometryGroup FillRule="Nonzero">
                        <!--绘制直线-->
                        <LineGeometry StartPoint="10,20" EndPoint="100,20" />
                        <!--绘制矩形-->
                        <RectangleGeometry Rect="10,50,100,50" />
                        <!--绘制椭圆-->
                        <EllipseGeometry RadiusX="50" RadiusY="25" Center="60,50"/>
                    </GeometryGroup>
                </Path.Data>
            </Path>
        </StackPanel>
        <StackPanel Canvas.Top="10" Canvas.Left="300">
            <TextBlock Text="EvenOdd方式填充图形"></TextBlock>
            <Path StrokeThickness="1" Fill="Yellow"  Stroke="Black" Margin="5" >
                <Path.Data  >
                    <!--使用GeometryGroup组合集合图形-->
                    <GeometryGroup FillRule="EvenOdd">
                        <!--绘制直线-->
                        <LineGeometry StartPoint="10,20" EndPoint="100,20" />
                        <!--绘制矩形-->
                        <RectangleGeometry Rect="10,50,100,50" />
                        <!--绘制椭圆-->
                        <EllipseGeometry RadiusX="50" RadiusY="25" Center="60,50"/>
                    </GeometryGroup>
                </Path.Data>
            </Path>
        </StackPanel>
    </Canvas>
</Window>

使用CombinedGeometry结合形状

使用GeometryCombineMode的枚举属性可以为组合图形应用一些布尔运算。

Union:通过采用两个区域的并集合并两个区域。新的图形为两个图形。

Inntersect:通过采用两个区域的交集合并两个区域。新的图形为两个图形相交部分。

Xor:将在第一个图形中但不在第二个图形中的区域,和在第二个图形但不在第一个图形的区域进行合并。新的区域为(A-B)+(B-A)组成。

Exclude:从第一个图形总除去第二个图形。

<Window x:Class="WPFDemo.CombinedGeometryDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFDemo"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="CombinedGeometryDemo" Height="530" Width="500">
    <Canvas>
        <StackPanel Canvas.Left="10" Canvas.Top="10">
            <TextBlock Text="Union计算组合图形" />
            <Path Stroke="Black" StrokeThickness="1" Fill="Yellow" >
                <Path.Data>
                    <!---使用Union组合多个图形-->
                    <CombinedGeometry GeometryCombineMode="Union">
                        <CombinedGeometry.Geometry1>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="75,75"/>
                        </CombinedGeometry.Geometry1>
                        <CombinedGeometry.Geometry2>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="125,75"/>
                        </CombinedGeometry.Geometry2>
                    </CombinedGeometry>
                </Path.Data>
            </Path>
        </StackPanel>
        <StackPanel Canvas.Left="250" Canvas.Top="10">
            <TextBlock Text="Exclude计算组合图形" />
            <Path Stroke="Black" StrokeThickness="1" Fill="Yellow" >
                <Path.Data>
                    <!---使用Exclude组合多个图形-->
                    <CombinedGeometry GeometryCombineMode="Exclude">
                        <CombinedGeometry.Geometry1>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="75,75"/>
                        </CombinedGeometry.Geometry1>
                        <CombinedGeometry.Geometry2>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="125,75"/>
                        </CombinedGeometry.Geometry2>
                    </CombinedGeometry>
                </Path.Data>
            </Path>
        </StackPanel>
        <StackPanel Canvas.Left="10" Canvas.Top="250">
            <TextBlock Text="Intersect计算组合图形" />
            <Path Stroke="Black" StrokeThickness="1" Fill="Yellow" >
                <Path.Data>
                    <!---使用Intersect组合多个图形-->
                    <CombinedGeometry GeometryCombineMode="Intersect">
                        <CombinedGeometry.Geometry1>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="75,75"/>
                        </CombinedGeometry.Geometry1>
                        <CombinedGeometry.Geometry2>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="125,75"/>
                        </CombinedGeometry.Geometry2>
                    </CombinedGeometry>
                </Path.Data>
           </Path>
        </StackPanel>

       <StackPanel Canvas.Left="250" Canvas.Top="250">
            <TextBlock Text="Xor计算组合图形" />
            <Path Stroke="Black" StrokeThickness="1" Fill="Yellow" >
                <Path.Data>
                    <!---使用Xor组合多个图形-->
                    <CombinedGeometry GeometryCombineMode="Xor">
                        <CombinedGeometry.Geometry1>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="75,75"/>
                        </CombinedGeometry.Geometry1>
                        <CombinedGeometry.Geometry2>
                            <EllipseGeometry  RadiusX="50" RadiusY="50" Center="125,75"/>
                        </CombinedGeometry.Geometry2>
                    </CombinedGeometry>
                </Path.Data>
            </Path>
        </StackPanel>
    </Canvas>
</Window>

PathGeometry对象

PathGeometry对象是集合图形中最强大的元素,使用该对象可以绘制弧形、曲线、椭圆、直线和矩形等组成的复杂图形。每个PathGeomery对象都使用一个和多个PathFigure对象,该对象存储在PathGeometry.Figures集合中。每个PathFigure对象都可以由一个或多个PathSegment对象组成。每个PathGeomery对象都使用一个和多个PathFigure对象,该对象存在PaathGeometryFigures集合中。每个PathFigure对象都有一个或多个PathSegment对象组成。

PathFigure的重要属性:

StartPoint:指定线段的起点

Segments:一个PathSegment对象的集合,用于绘制图形。

IsClosed:如果设置为true,将添加一个直线连接起点和终点。

IsFilled:如果设置为true,图形的内部区域将使用Path.Fill画刷填充。

PathSegment派生类:

LineSegment:在两个点之间创建直线。

ArcSegment:在两个点之间创建圆弧。

BezierSegment:在两个点之间创建贝塞尔曲线。

QuadraticBezierSegment:在PathFigure的两点之间创建一条二次赛贝尔曲线。

PolyLineSegment:创建一系列直线,可以使用多个LineSegment对象获得同样的效果,但是使用polyLineSegment更简单。

PolyBeeierSegment:创建一条或多条三次贝塞尔曲线。

PolyQuadraticBezierSegment:创建一系列二次贝塞尔线段。

使用PathGeeometry绘制图形

<Window x:Class="WPFDemo.PathGeometryDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFDemo"
        mc:Ignorable="d"
        Title="PathGeometryDemo" Height="350" Width="700">
    <Canvas>
        <!--绘制直线-->
        <StackPanel Canvas.Top="10" Canvas.Left="10">
            <TextBlock Text="绘制直线IsClose设置为False"/>
            <Path Stroke="Blue">
                <Path.Data>
                    <PathGeometry>
                        <PathFigure IsClosed="True" StartPoint="10,10" >
                            <!--使用LineSegment绘制直线-->
                            <LineSegment  Point="10,100"/>
                            <LineSegment  Point="100,50"/>
                        </PathFigure>
                    </PathGeometry>
                </Path.Data>
            </Path>
        </StackPanel>
        <StackPanel Canvas.Top="150" Canvas.Left="10">
            <TextBlock Text="绘制直线IsClose设置为False"/>
            <Path Stroke="Blue">
                <Path.Data>
                    <PathGeometry>
                        <PathFigure IsClosed="False" StartPoint="10,10" >
                            <!--使用LineSegment绘制直线-->
                            <LineSegment  Point="10,100"/>
                            <LineSegment  Point="100,50"/>
                        </PathFigure>
                    </PathGeometry>
                </Path.Data>
            </Path>
        </StackPanel>

        <!--绘制弧线-->
        <StackPanel Canvas.Left="200" Canvas.Top="10">
            <Path Stroke="Black" StrokeThickness="1">
              <Path.Data>
                    <PathGeometry>
                        <PathGeometry.Figures>
                            <PathFigureCollection>
                                <PathFigure StartPoint="10,10">
                                    <PathFigure.Segments>
                                        <PathSegmentCollection>
                                            <!--绘制弧线-->
                                            <ArcSegment Size="100,50" RotationAngle="45" IsLargeArc="True" SweepDirection="Counterclockwise" Point="200,100"></ArcSegment>
                                        </PathSegmentCollection>
                                    </PathFigure.Segments>
                                </PathFigure>
                            </PathFigureCollection>
                        </PathGeometry.Figures>
                    </PathGeometry>
                </Path.Data>
            </Path>
        </StackPanel>

       <!--绘制贝塞尔曲线-->
        <StackPanel Canvas.Top="10" Canvas.Left="400">
            <Path Stroke="Black" StrokeThickness="5" >
                <Path.Data>
                    <PathGeometry>
                        <PathFigure StartPoint="10,10">
                            <!--绘制贝塞尔曲线-->
                            <BezierSegment Point1="130,30" Point2="40,140" Point3="150,150" />
                        </PathFigure>
                    </PathGeometry>
                </Path.Data>
            </Path>
        </StackPanel>
    </Canvas>
</Window>

时间: 2024-08-03 04:26:57

WPF 10天修炼 第九天 - 几何图形的相关文章

WPF 10天修炼 第十天- WPF数据绑定

WPF数据绑定 数据绑定到元素属性是将源对象指定为一个WPF元素,并且源属性是一个依赖属性,依赖属性内置了变更通知.当改变源对象依赖属性值之后,绑定目标可以立即得到更新,开发人员不需要手动编写响应事件. 在绑定来源和绑定目标之间,可以使用Mode属性指定绑定的方法.Mode属性是System.Windows.Data.BindMode枚举类型的属性: OneWay:源数据变更目标数据变更,反之不行 OneTime:仅在启动时更新 OneWayToSource:目标数据更新源数据更新,反之不行 T

WPF 10天修炼 第八天 - 形状、画刷和变换

图形 在WPF中使用绘图最简单的就是使用Shape类.Shape类继承自FrameworkElement,是一个专门用来绘图的类.Shape类中年派生的类有直线.矩形.多边形和圆形等. System.Windows.Shapes.Shape类是一个抽象类,从该类又派生出多个不同的子类,如下图: Shape类的通用属性 属性名称 描述 Fill 绘制填充的画刷 Stroke 绘制边框的画刷 StrokeThickness 与设备无关的边框宽度 StrokeStartLineCap和StrokeEn

WPF 10天修炼 - WPF布局容器

WPF布局 WPF的窗口也就是Window类,是一个内容控件,该控件派生自ContentControl.内容控件有一个Content属性,该属性有一个限制,只能放置一个用户界面元素,或一个字符串.为了在窗口上放置多个界面控件,通常在窗口上放置一个容器控件. WFP布局原则 1.  元素不应该指定 确定的尺寸大小,同很惨更应该使其大小自动适应内容.比如按钮根据所添加的文本来扩展其大小.可以通过设置maximun和minimun尺寸来限制控件可接受的尺寸大小. 2.  元素不应该使用屏幕坐标来指定其

WPF 10天修炼 - 内容控件

WPF内容控件 在WPF中,所有呈现在用户界面上的对象都称为用户界面元素.但是只有派生自System.Windows.Controls.Control类的对象才称为控件.内容控件通常是指具有Content属性的控件,Content属性并非定义在每个控件中,而是定义在基类System.Windows.Controls命名空间的ContentControl类中.注意:Content属性只接收单个内容元素. WPF内容控件分类 1.  直接继承ContentControl的控件 2.  继承Heade

WPF 10天修炼 - Application全局应用程序类

Application对象 当一个WPF应用程序启动时,首先会实例化一个全局唯一的Application对象,类似于WinForm下的Application类,用于控制整个应用程序,该类将用于追踪应用程序打开的窗口.在应用程序打开或关闭的时候能够触发相应的事件. 创建Application对象 手动创建Application应用程序对象过程: 1.  使用VS创建WPF应用程序,命名为WPFDemo.然后手动清除App.xaml文件. 2.  添加Startup.cs类,并添加程序代码.如下:

2015.10.19 福州大学第九届程序设计竞赛

FZU 2086 餐厅点餐 枚举 练的时候以为是dp---(事实上这场只做了10来分钟---就愉快地滚去吃饭了---) 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int A,B,C,D,E; 7 int a[15];//汤 8 int b[15];//饭 9 int c[15];//面 1

10.06 国庆节第九场模拟赛

密钥(key) Description 在这个问题中,一个密钥是指一个长度为\(3n\)的二进制序列,其中\(n\)是正整数. 序列的每一位从左往右依次被编号为\(1\)到\(3n\) ,一个密钥的权值是指数字不同的相邻位的个数再加上\(1\) .比如: \(000\) 的权值是 \(1\), \(011010100\) 的权值是 \(7\). 密钥可以被修改.确切地说,你可以不断地进行下面的操作:任选两个相邻的位,然后同时将它们取反.例如,可以通过一次操作把 \(000\) 修改为 110 .

Java闭关修炼64课 很适合新手学习的JAVA视频教程

Java闭关修炼64课 很适合新手学习的JAVA视频教程 java闭关修炼第一课 什么是java(1).rar  java闭关修炼第一课 什么是java.rar  java闭关修炼第七课 基础语言要素(1).rar  java闭关修炼第七课 基础语言要素.rar  java闭关修炼第三十一课 静态方法(1).rar  java闭关修炼第三十一课 静态方法.rar  java闭关修炼第三十七课 成员初始化顺序讨论(1).rar  java闭关修炼第三十七课 成员初始化顺序讨论.rar  java闭

2017-2018-1 20155228 《信息安全系统设计基础》第九周学习总结

2017-2018-1 20155228 <信息安全系统设计基础>第九周学习总结 教材学习内容总结 常见的存储技术 RAM 随机访问存储器(Random-Access Memory, RAM)分为两类:静态的和动态的.静态 RAM(SRAM)比动态RAM(DRAM)更快,但也贵得多.SRAM用来作为高速缓存存储 器,既可以在CPU芯片上,也可以在片下.DRAM用来作为主存以及图形系统的帧缓冲 区.典型地,一个桌面系统的SRAM不会超过几兆字节,但是DRAM却有几百或几千兆 字节. SRAM将每