本主题概述了如何使用Shape对象进行绘图。 Shape 是一种 UIElement,用于将形状绘制到屏幕上。 由于它们是 UI 元素,Shape 对象可以在 Panel 元素和大多数控件内使用。
Windows Presentation Foundation (WPF)提供对图形和渲染服务的多层访问。 在顶层, Shape 对象易于使用并提供许多有用的功能,例如布局和参与 Windows Presentation Foundation (WPF) 事件系统。
形状相关的类型位于命名空间中 Windows.Shapes
。 与几何图形相关的类型位于命名空间中 System.Windows.Media
。
形状对象
WPF 提供了许多随时可用的 Shape 对象。 所有形状对象都继承自 Shape 类。 可用的形状对象包括Ellipse、、LinePath、Polygon和PolylineRectangle。 Shape 对象共享以下常见属性。
Stroke:描述如何绘制形状的轮廓。
StrokeThickness:描述形状轮廓的粗细。
Fill:描述形状的内部如何绘制。
用于指定坐标和顶点的数据属性,以与设备无关的像素为单位。
由于它们派生自 UIElement,因此可以在面板和大多数控件内使用形状对象。 面板 Canvas 是创建复杂绘图的特别好选择,因为它支持对其子对象进行绝对定位。
类 Line 使你能够在两个点之间绘制一条线。 以下示例演示了指定线条坐标和笔划属性的几种方法。
<Canvas Height="300" Width="300">
<!-- Draws a diagonal line from (10,10) to (50,50). -->
<Line
X1="10" Y1="10"
X2="50" Y2="50"
Stroke="Black"
StrokeThickness="4" />
<!-- Draws a diagonal line from (10,10) to (50,50)
and moves it 100 pixels to the right. -->
<Line
X1="10" Y1="10"
X2="50" Y2="50"
StrokeThickness="4"
Canvas.Left="100">
<Line.Stroke>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Blue" Offset="0.25" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Line.Stroke>
</Line>
<!-- Draws a horizontal line from (10,60) to (150,60). -->
<Line
X1="10" Y1="60"
X2="150" Y2="60"
Stroke="Black"
StrokeThickness="4"/>
</Canvas>
// Add a Line Element
myLine = gcnew Line();
myLine->Stroke = Brushes::LightSteelBlue;
myLine->X1 = 1;
myLine->X2 = 50;
myLine->Y1 = 1;
myLine->Y2 = 50;
myLine->HorizontalAlignment = HorizontalAlignment::Left;
myLine->VerticalAlignment = VerticalAlignment::Center;
myLine->StrokeThickness = 2;
myGrid->Children->Add(myLine);
// Add a Line Element
myLine = new Line();
myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
myLine.X1 = 1;
myLine.X2 = 50;
myLine.Y1 = 1;
myLine.Y2 = 50;
myLine.HorizontalAlignment = HorizontalAlignment.Left;
myLine.VerticalAlignment = VerticalAlignment.Center;
myLine.StrokeThickness = 2;
myGrid.Children.Add(myLine);
' Add a Line Element
Dim myLine As New Line()
myLine.Stroke = Brushes.LightSteelBlue
myLine.X1 = 1
myLine.X2 = 50
myLine.Y1 = 1
myLine.Y2 = 50
myLine.HorizontalAlignment = HorizontalAlignment.Left
myLine.VerticalAlignment = VerticalAlignment.Center
myLine.StrokeThickness = 2
myGrid.Children.Add(myLine)
下图显示了呈现的 Line。
尽管该 Line 类确实提供属性 Fill ,但设置它不起作用,因为 Line 没有区域。
另一个常见的形状是 Ellipse。 创建一个Ellipse,通过定义形状的Width和Height属性。 若要绘制圆圈,请指定 Ellipse,使 Width 和 Height 的值相等。
<Ellipse
Fill="Yellow"
Height="100"
Width="200"
StrokeThickness="2"
Stroke="Black"/>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace SDKSample
{
public partial class SetBackgroundColorOfShapeExample : Page
{
public SetBackgroundColorOfShapeExample()
{
// Create a StackPanel to contain the shape.
StackPanel myStackPanel = new StackPanel();
// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();
// Create a SolidColorBrush with a red color to fill the
// Ellipse with.
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.Black;
// Set the width and height of the Ellipse.
myEllipse.Width = 200;
myEllipse.Height = 100;
// Add the Ellipse to the StackPanel.
myStackPanel.Children.Add(myEllipse);
this.Content = myStackPanel;
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Namespace SDKSample
Partial Public Class SetBackgroundColorOfShapeExample
Inherits Page
Public Sub New()
' Create a StackPanel to contain the shape.
Dim myStackPanel As New StackPanel()
' Create a red Ellipse.
Dim myEllipse As New Ellipse()
' Create a SolidColorBrush with a red color to fill the
' Ellipse with.
Dim mySolidColorBrush As New SolidColorBrush()
' Describes the brush's color using RGB values.
' Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0)
myEllipse.Fill = mySolidColorBrush
myEllipse.StrokeThickness = 2
myEllipse.Stroke = Brushes.Black
' Set the width and height of the Ellipse.
myEllipse.Width = 200
myEllipse.Height = 100
' Add the Ellipse to the StackPanel.
myStackPanel.Children.Add(myEllipse)
Me.Content = myStackPanel
End Sub
End Class
End Namespace
下图显示了呈现 Ellipse的示例。
使用路径和几何图形
类 Path 使你能够绘制曲线和复杂形状。 这些曲线和形状是使用 Geometry 对象描述的。 若要使用 a Path,请创建 Geometry 并使用它来设置 Path 对象的 Data 属性。
有多种 Geometry 对象可供选择。 和LineGeometryRectangleGeometryEllipseGeometry类描述相对简单的形状。 若要创建更复杂的形状或创建曲线,请使用 .PathGeometry
路径几何 (PathGeometry) 和路径片段 (PathSegments)
PathGeometry 对象由一个或多个 PathFigure 对象组成;每个 PathFigure 对象表示不同的“图形”或形状。 每个 PathFigure 对象本身都由一个或多个 PathSegment 对象组成,每个对象表示图形或形状的连接部分。 段类型包括:LineSegment、BezierSegment和ArcSegment。
在下面的示例中,一个 Path 用于绘制二次贝塞尔曲线。
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="10,100">
<PathFigure.Segments>
<PathSegmentCollection>
<QuadraticBezierSegment Point1="200,200" Point2="300,100" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
下图显示了呈现的形状。
有关PathGeometry和其他Geometry类的更多信息,请参阅几何概述。
XAML 缩写语法
在可扩展应用程序标记语言(XAML)中,还可以使用特殊的缩写语法来描述一个 Path。 在以下示例中,缩写语法用于绘制复杂形状。
<Path Stroke="DarkGoldenRod" StrokeThickness="3"
Data="M 100,200 C 100,25 400,350 400,175 H 280" />
下图展示了渲染的 Path。
Data特性字符串以 M 指示的“moveto”命令开头,该命令为坐标系统中Canvas的路径建立起点。 Path 数据参数区分大小写。 大写 M 表示新当前点的绝对位置。 小写 m 表示相对坐标。 第一段是立方贝塞尔曲线,起始于(100,200),结束于(400,175),通过两个控制点(100,25)和(400,350)绘制。 此段由属性字符串中的 Data C 命令指示。 同样,大写 C 表示绝对路径;小写 c 表示相对路径。
第二段以绝对水平“lineto”命令 H 开头,它指定从前一个子路径的终结点(400,175)到新终结点(280,175)绘制的线条。 因为它是水平“lineto”命令,因此指定的值为 x 坐标。
有关完整的路径语法,请参阅 Data 参考文档,并通过使用 PathGeometry 创建形状。
绘制形状
Brush 对象用于绘制形状的 Stroke 和 Fill。 在以下示例中,指定了 Ellipse 的笔划和填充。 请注意,画笔属性的有效输入可以是关键字或十六进制颜色值。 有关可用颜色关键字的详细信息,请参阅命名空间中System.Windows.Media类的属性Colors。
<Canvas Background="LightGray">
<Ellipse
Canvas.Top="50"
Canvas.Left="50"
Fill="#FFFFFF00"
Height="75"
Width="75"
StrokeThickness="5"
Stroke="#FF0000FF"/>
</Canvas>
下图显示了呈现的 Ellipse。
或者,可以使用属性元素语法显式创建对象 SolidColorBrush 以使用纯色绘制形状。
<!-- This polygon shape uses pre-defined color values for its Stroke and
Fill properties.
The SolidColorBrush's Opacity property affects the fill color in
this case by making it slightly transparent (opacity of 0.4) so
that it blends with any underlying color. -->
<Polygon
Points="300,200 400,125 400,275 300,200"
Stroke="Purple"
StrokeThickness="2">
<Polygon.Fill>
<SolidColorBrush Color="Blue" Opacity="0.4"/>
</Polygon.Fill>
</Polygon>
下图显示了呈现的形状。
还可以绘制形状的笔划或填充渐变、图像、图案等。 有关详细信息,请参阅 “使用纯色和渐变绘制”概述。
可拉伸形状
Line、Path、PolygonPolyline和Rectangle类都具有一个Stretch属性。 此属性确定对象的内容(要绘制的形状)如何被 Shape 拉伸以填充 Shape 对象的布局空间。 Shape对象的布局空间是指布局系统为Shape分配的空间量,这可能是因为有明确的Width和Height设置,或者是因为其HorizontalAlignment和VerticalAlignment设置。 有关 Windows Presentation Foundation 中的布局的其他信息,请参阅 布局 概述。
Stretch 属性采用以下值之一:
UniformToFill:将 Shape 对象的内容拉伸以完全填充其布局空间,同时保留其原始纵横比。
请注意,当Shape对象的内容拉伸时,Shape对象的轮廓是在拉伸后绘制的。
在下面的示例中,一个 Polygon 用于绘制一个非常小的三角形,从 (0,0) 到 (0,1) 到 (1,1)。 Polygon对象的Width和Height设置为 100,其拉伸属性设置为 Fill。 因此,对象的 Polygon 内容(三角形)被拉伸以填充更大的空间。
<Polygon
Points="0,0 0,1 1,1"
Fill="Blue"
Width="100"
Height="100"
Stretch="Fill"
Stroke="Black"
StrokeThickness="2" />
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(new Point(0,0));
myPointCollection.Add(new Point(0,1));
myPointCollection.Add(new Point(1,1));
Polygon myPolygon = new Polygon();
myPolygon.Points = myPointCollection;
myPolygon.Fill = Brushes.Blue;
myPolygon.Width = 100;
myPolygon.Height = 100;
myPolygon.Stretch = Stretch.Fill;
myPolygon.Stroke = Brushes.Black;
myPolygon.StrokeThickness = 2;
形状变换
该 Transform 类提供了在二维平面中转换形状的方法。 不同类型的转换包括旋转()、刻度(RotateTransformScaleTransform)、倾斜(SkewTransform)和翻译(TranslateTransform)。
要应用于形状的常见转换是旋转。 若要旋转形状,请创建 RotateTransform 并指定其旋转参数 Angle。 一个角度的 Angle 使元素顺时针旋转 45 度;90 度的角度使元素顺时针旋转 90 度;依此类推。 要控制元素旋转的中心点,请设置CenterX和CenterY属性。 这些属性值以正在转换的元素的坐标空间表示。 CenterX 默认值 CenterY 为零。 最后,将元素 RotateTransform 应用到该元素。 如果不希望转换影响布局,请设置形状 RenderTransform 的属性。
在以下示例中,使用 RotateTransform 将形状围绕其左上角(0,0)旋转 45 度。
<!-- Rotates the Polyline 45 degrees about the point (0,0). -->
<Polyline Points="25,25 0,50 25,75 50,50 25,25 25,0"
Stroke="Blue" StrokeThickness="10"
Canvas.Left="75" Canvas.Top="50">
<Polyline.RenderTransform>
<RotateTransform CenterX="0" CenterY="0" Angle="45" />
</Polyline.RenderTransform>
</Polyline>
在下一个示例中,另一个形状旋转 45 度,但这次是在点 (25,50) 进行旋转。
<!-- Rotates the Polyline 45 degrees about its center. -->
<Polyline
Points="25,25 0,50 25,75 50,50 25,25 25,0"
Stroke="Blue" StrokeThickness="10"
Canvas.Left="75" Canvas.Top="50"
RenderTransformOrigin="0.5,0.5">
<Polyline.RenderTransform>
<RotateTransform Angle="45" />
</Polyline.RenderTransform>
</Polyline>
下图显示了应用两个转换的结果。
在前面的示例中,将单个转换应用于每个形状对象。 若要对形状(或任何其他 UI 元素)应用多个转换,请使用一个 TransformGroup。