WPF系列十一:图形控件RectangleGeometry

打印 上一主题 下一主题

主题 876|帖子 876|积分 2628

简介

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


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

属性



  • Rect:指定矩形的位置(左上角坐标)和大小(宽度和高度)。这个属性接受一个 Rect 布局体,该布局体定义了矩形的边界框。
  • RadiusX 和 RadiusY:这两个属性分别指定了矩形四个角的水平半径和垂直半径。当它们被设置为非零值时,矩形的角落会被圆滑处理。RadiusX 和 RadiusY 的最大值分别是矩形宽度的一半和高度的一半。如果设定了这些值,则矩形将拥有圆角。
  • Transform:允许对 RectangleGeometry 应用变换,如旋转、缩放、平移等。这使得你可以轻松地调解矩形的位置或改变其表面而不必修改原始多少数据。

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


示例

绘制一个带圆角的矩形


代码:
  1. <Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WPFDemo.Line.Views"
  7.         mc:Ignorable="d"
  8.         Title="RectangleGeometryWindow" Height="450" Width="800">
  9.     <Grid>
  10.         <!--定义一个带圆角的矩形-->
  11.         <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
  12.             <Path.Data>
  13.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
  14.             </Path.Data>
  15.         </Path>
  16.     </Grid>
  17. </Window>
复制代码
效果:


平移变换(TranslateTransform)

代码:
  1. <Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow1"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WPFDemo.Line.Views"
  7.         mc:Ignorable="d"
  8.         Title="RectangleGeometryWindow1" Height="450" Width="800">
  9.     <Grid>
  10.         <!--定义一个带圆角的矩形-->
  11.         <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
  12.             <Path.Data>
  13.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
  14.             </Path.Data>
  15.         </Path>
  16.         <!--平移变换-->
  17.         <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
  18.             <Path.Data>
  19.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
  20.                     <RectangleGeometry.Transform>
  21.                         <TranslateTransform X="100" Y="0"/>
  22.                     </RectangleGeometry.Transform>
  23.                 </RectangleGeometry>
  24.             </Path.Data>
  25.         </Path>
  26.     </Grid>
  27. </Window>
复制代码
效果:


旋变化换(ScaleTransform)

代码:
  1. <Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow2"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WPFDemo.Line.Views"
  7.         mc:Ignorable="d"
  8.         Title="RectangleGeometryWindow2" Height="450" Width="800">
  9.     <Grid>
  10.         <!--定义一个带圆角的矩形-->
  11.         <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
  12.             <Path.Data>
  13.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
  14.             </Path.Data>
  15.         </Path>
  16.         <!--旋转变换-->
  17.         <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
  18.             <Path.Data>
  19.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
  20.                     <RectangleGeometry.Transform>
  21.                         <RotateTransform CenterX="300" CenterY="100" Angle="90"/>
  22.                     </RectangleGeometry.Transform>
  23.                 </RectangleGeometry>
  24.             </Path.Data>
  25.         </Path>
  26.     </Grid>
  27. </Window>
复制代码
效果:


缩放变换(RotateTransform)

代码:
  1. <Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow3"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WPFDemo.Line.Views"
  7.         mc:Ignorable="d"
  8.         Title="RectangleGeometryWindow3" Height="450" Width="800">
  9.     <Grid>
  10.         <!--定义一个带圆角的矩形-->
  11.         <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
  12.             <Path.Data>
  13.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
  14.             </Path.Data>
  15.         </Path>
  16.         <!--缩放变换-->
  17.         <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
  18.             <Path.Data>
  19.                 <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
  20.                     <RectangleGeometry.Transform>
  21.                         <ScaleTransform ScaleX="0.8" ScaleY="0.8" />
  22.                     </RectangleGeometry.Transform>
  23.                 </RectangleGeometry>
  24.             </Path.Data>
  25.         </Path>
  26.     </Grid>
  27. </Window>
复制代码
效果:


组合变换(TransformGroup)


代码:
  1. <Window x:Class="WPFDemo.Line.Views.RectangleGeometryWindow4"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WPFDemo.Line.Views"
  7.         mc:Ignorable="d"
  8.         Title="RectangleGeometryWindow4" Height="450" Width="800">
  9.     <Grid>
  10.         <Grid>
  11.             <!--定义一个带圆角的矩形-->
  12.             <Path Stroke="Red" StrokeThickness="2" Fill="Yellow">
  13.                 <Path.Data>
  14.                     <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20"/>
  15.                 </Path.Data>
  16.             </Path>
  17.             <!--组合变换-->
  18.             <Path Stroke="Blue" StrokeThickness="2" Fill="Green">
  19.                 <Path.Data>
  20.                     <RectangleGeometry Rect="300,150,200,100" RadiusX="20"  RadiusY="20">
  21.                         <RectangleGeometry.Transform>
  22.                             <TransformGroup>
  23.                                 <TranslateTransform X="100" Y="0"/>
  24.                                 <RotateTransform CenterX="300" CenterY="100" Angle="90"/>
  25.                                 <ScaleTransform ScaleX="0.8" ScaleY="0.8" />
  26.                             </TransformGroup>
  27.                         </RectangleGeometry.Transform>
  28.                     </RectangleGeometry>
  29.                 </Path.Data>
  30.             </Path>
  31.         </Grid>
  32.     </Grid>
  33. </Window>
复制代码
效果:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万有斥力

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表