WPF系列十二:图形控件CombinedGeometry

简介

CombinedGeometry 是 WPF (Windows Presentation Foundation) 中的一个几何对象,用于将两个几何图形组合成一个新的几何图形。它允许你通过不同的组合模式(如相交、并集、差集或异或)来创建复杂的形状。常与 Path 控件一起使用来绘制组合后的图形。

  • 几何组合:可以将两个几何对象按照指定的方式进行组合,产生新的几何形状。
  • 参与绘制 :作为 Path 元素的数据源,用来绘制复杂图形。
  • 与其他几何对象组合:能够进一步与其他几何对象结合,创建更复杂的图形设计。

属性

  • GeometryCombineMode :定义了两个几何图形如何组合,有四种模式:

    • Union:两个几何图形的并集,即合并所有区域。
    • Intersect:两个几何图形的交集,只保留重叠部分。
    • Xor:两个几何图形的异或,保留非重叠部分。
    • Exclude:从第一个几何图形中排除第二个几何图形覆盖的区域。
  • Geometry1 和 Geometry2 :分别表示要组合的第一个和第二个几何对象。它们可以是任何几何类型,例如 EllipseGeometry, RectangleGeometry, LineGeometry 等。

  • Transform:允许对 RectangleGeometry 应用变换,如旋转、缩放、平移等。这使得你可以轻松地调整矩形的位置或改变其外观而不必修改原始几何数据。

    • TranslateTransform:用于移动(平移)对象。
    • ScaleTransform:用于缩放对象。
    • RotateTransform:用于旋转对象。
    • SkewTransform:用于倾斜对象。
    • MatrixTransform:使用矩阵来定义更复杂的变换。
    • TransformGroup: 组合变换。

示例

组合矩形和椭圆绘制一个灯笼

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.CombinedGeometry"
        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.Line.Views"
        mc:Ignorable="d"
        Title="CombinedGeometry" Height="450" Width="800">
    <Grid>
        <!--组合矩形和椭圆绘制一个灯笼-->
        <Path Stroke="Yellow" StrokeThickness="2" Fill="Red">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

平移变换(TranslateTransform)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.CombinedGeometry1"
        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.Line.Views"
        mc:Ignorable="d"
        Title="CombinedGeometry1" Height="450" Width="800">
    <Grid>
        <!--组合矩形和椭圆绘制一个灯笼-->
        <Path Stroke="Yellow" StrokeThickness="2" Fill="Red">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

        <!--平移变换-->
        <Path Stroke="YellowGreen" StrokeThickness="2" Fill="PaleVioletRed">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                    <CombinedGeometry.Transform>
                        <TranslateTransform X="200" Y="0"/>
                    </CombinedGeometry.Transform>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

旋转变换(ScaleTransform)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.CombinedGeometry2"
        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.Line.Views"
        mc:Ignorable="d"
        Title="CombinedGeometry2" Height="450" Width="800">
    <Grid>
        <!--组合矩形和椭圆绘制一个灯笼-->
        <Path Stroke="Yellow" StrokeThickness="2" Fill="Red">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

        <!--旋转变换-->
        <Path Stroke="YellowGreen" StrokeThickness="2" Fill="PaleVioletRed">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                    <CombinedGeometry.Transform>
                        <RotateTransform CenterX="300" CenterY="100" Angle="90"/>
                    </CombinedGeometry.Transform>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

缩放变换(RotateTransform)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.CombinedGeometry3"
        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.Line.Views"
        mc:Ignorable="d"
        Title="CombinedGeometry3" Height="450" Width="800">
    <Grid>
        <!--组合矩形和椭圆绘制一个灯笼-->
        <Path Stroke="Yellow" StrokeThickness="2" Fill="Red">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

        <!--缩放变换-->
        <Path Stroke="YellowGreen" StrokeThickness="2" Fill="PaleVioletRed">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                    <CombinedGeometry.Transform>
                        <ScaleTransform ScaleX="0.8" ScaleY="0.8" />
                    </CombinedGeometry.Transform>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

组合变换(TransformGroup)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.CombinedGeometry4"
        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.Line.Views"
        mc:Ignorable="d"
        Title="CombinedGeometry4" Height="450" Width="800">
    <Grid>
        <!--组合矩形和椭圆绘制一个灯笼-->
        <Path Stroke="Yellow" StrokeThickness="2" Fill="Red">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>

        <!--组合变换-->
        <Path Stroke="YellowGreen" StrokeThickness="2" Fill="PaleVioletRed">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry  Center="400,200" RadiusX="200" RadiusY="100"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry  Rect="300,200,200,110"/>
                    </CombinedGeometry.Geometry2>
                    <CombinedGeometry.Transform>
                        <TransformGroup>
                            <TranslateTransform X="200" Y="0"/>
                            <RotateTransform CenterX="300" CenterY="100" Angle="90"/>
                            <ScaleTransform ScaleX="0.8" ScaleY="0.8" />
                        </TransformGroup>
                    </CombinedGeometry.Transform>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

相关推荐
wangnaisheng16 小时前
【WPF】Opacity 属性的使用
wpf
姬激薄20 小时前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)21 小时前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心1 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo2 天前
9.1.领域驱动设计
wpf
大道随心2 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠2 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go2 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet3 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_3 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式