WPF系列十一:图形控件RectangleGeometry

简介

RectangleGeometry 是 WPF (Windows Presentation Foundation) 中用于定义矩形几何形状的一个几何对象。RectangleGeometry 通常与 Path 控件一起使用来绘制矩形,并且可以用来创建具有圆角的矩形。

  • 定义矩形RectangleGeometry 主要用于描述一个矩形区域,可以通过设置矩形的位置和尺寸来定义。
  • 参与绘制 :虽然 RectangleGeometry 本身不直接绘制任何内容,但它作为 Path 元素的数据源,允许你通过 Path 来绘制矩形。
  • 支持圆角 :可以通过设置 RadiusXRadiusY 属性来创建带有圆角的矩形。

属性

  • Rect :指定矩形的位置(左上角坐标)和大小(宽度和高度)。这个属性接受一个 Rect 结构体,该结构体定义了矩形的边界框。
  • RadiusX 和 RadiusY :这两个属性分别指定了矩形四个角的水平半径和垂直半径。当它们被设置为非零值时,矩形的角落会被圆滑处理。RadiusXRadiusY 的最大值分别是矩形宽度的一半和高度的一半。如果设定了这些值,则矩形将拥有圆角。
  • Transform :允许对 RectangleGeometry 应用变换,如旋转、缩放、平移等。这使得你可以轻松地调整矩形的位置或改变其外观而不必修改原始几何数据。
    • TranslateTransform:用于移动(平移)对象。
    • ScaleTransform:用于缩放对象。
    • RotateTransform:用于旋转对象。
    • SkewTransform:用于倾斜对象。
    • MatrixTransform:使用矩阵来定义更复杂的变换。
    • **TransformGroup:**组合变换。

示例

绘制一个带圆角的矩形

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow"
        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="RectangleGeometryWindow" Height="450" Width="800">
    <Grid>
        <!--定义一个带圆角的矩形-->
        <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

平移变换(TranslateTransform)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow1"
        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="RectangleGeometryWindow1" Height="450" Width="800">
    <Grid>
        <!--定义一个带圆角的矩形-->
        <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
            </Path.Data>
        </Path>

        <!--平移变换-->
        <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
                    <RectangleGeometry.Transform>
                        <TranslateTransform X="100" Y="0"/>
                    </RectangleGeometry.Transform>
                </RectangleGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

旋转变换(ScaleTransform)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow2"
        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="RectangleGeometryWindow2" Height="450" Width="800">
    <Grid>
        <!--定义一个带圆角的矩形-->
        <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
            </Path.Data>
        </Path>

        <!--旋转变换-->
        <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
                    <RectangleGeometry.Transform>
                        <RotateTransform CenterX="300" CenterY="100" Angle="90"/>
                    </RectangleGeometry.Transform>
                </RectangleGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

缩放变换(RotateTransform)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow3"
        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="RectangleGeometryWindow3" Height="450" Width="800">
    <Grid>
        <!--定义一个带圆角的矩形-->
        <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
            </Path.Data>
        </Path>

        <!--缩放变换-->
        <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
            <Path.Data>
                <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
                    <RectangleGeometry.Transform>
                        <ScaleTransform ScaleX="0.8" ScaleY="0.8" />
                    </RectangleGeometry.Transform>
                </RectangleGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

效果:

组合变换(TransformGroup)

代码:

cs 复制代码
<Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow4"
        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="RectangleGeometryWindow4" Height="450" Width="800">
    <Grid>
        <Grid>
            <!--定义一个带圆角的矩形-->
            <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
                <Path.Data>
                    <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
                </Path.Data>
            </Path>

            <!--组合变换-->
            <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
                <Path.Data>
                    <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
                        <RectangleGeometry.Transform>
                            <TransformGroup>
                                <TranslateTransform X="100" Y="0"/>
                                <RotateTransform CenterX="300" CenterY="100" Angle="90"/>
                                <ScaleTransform ScaleX="0.8" ScaleY="0.8" />
                            </TransformGroup>
                        </RectangleGeometry.Transform>
                    </RectangleGeometry>
                </Path.Data>
            </Path>
        </Grid>
    </Grid>
</Window>

效果:

相关推荐
Macbethad16 小时前
EtherCAT从站程序技术方案:基于WPF的高性能实现
网络协议·wpf
Macbethad16 小时前
基于WPF的485主站系统技术方案
网络协议·wpf·信息与通信
赵财猫._.2 天前
HarmonyOS内存优化实战:泄漏检测、大对象管理与垃圾回收策略
华为·wpf·harmonyos
赵财猫._.2 天前
鸿蒙超级终端体验:无缝流转的底层实现与用户体验优化
wpf·harmonyos·ux
故事不长丨2 天前
C#委托的使用
c#·wpf·winfrom·委托·网站开发
行走正道2 天前
【探索实战】跨云应用分发自动化实战:基于Kurator的统一交付体系深度解析
运维·自动化·wpf·kurator·跨云分发
Macbethad2 天前
基于WPF的Ethernet/IP主站程序技术方案
网络协议·tcp/ip·wpf
张人玉2 天前
Prism Template Pack 完整使用示例(VS2022 + .NET 8 + DryIoc)
.net·wpf·prism
棉晗榜2 天前
wpf 在XAML中配置视图模型,通过 d:DataContext设置设计时类型,方便按F12跳转查看类型
wpf
赵财猫._.3 天前
HarmonyOS渲染性能优化:组件树复用与局部刷新机制
wpf·harmonyos·ux