共用方式為


HOW TO:建立複合圖案

本範例示範如何使用 Geometry 物件建立複合圖案,並使用 Path 項目顯示這些圖案。 下列範例將 LineGeometryEllipseGeometryRectangleGeometry 搭配使用 GeometryGroup 來建立複合圖案。 接著再使用 Path 項目繪製幾何圖形。

範例

<!-- Displays the geometry. --> 
<Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">
  <Path.Data>

    <!-- Creates a composite shape from three geometries. -->
    <GeometryGroup FillRule="EvenOdd">
      <LineGeometry StartPoint="10,10" EndPoint="50,30" />
      <EllipseGeometry Center="40,70" RadiusX="30" RadiusY="30" />              
      <RectangleGeometry Rect="30,55 100 30" />
    </GeometryGroup>
  </Path.Data>
</Path>
            ' Create a Path to be drawn to the screen.
            Dim myPath As New Path()
            myPath.Stroke = Brushes.Black
            myPath.StrokeThickness = 1
            Dim mySolidColorBrush As New SolidColorBrush()
            mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255)
            myPath.Fill = mySolidColorBrush

            ' Create the line geometry to add to the Path
            Dim myLineGeometry As New LineGeometry()
            myLineGeometry.StartPoint = New Point(10, 10)
            myLineGeometry.EndPoint = New Point(50, 30)

            ' Create the ellipse geometry to add to the Path
            Dim myEllipseGeometry As New EllipseGeometry()
            myEllipseGeometry.Center = New Point(40, 70)
            myEllipseGeometry.RadiusX = 30
            myEllipseGeometry.RadiusY = 30

            ' Create a rectangle geometry to add to the Path
            Dim myRectGeometry As New RectangleGeometry()
            myRectGeometry.Rect = New Rect(30, 55, 100, 30)

            ' Add all the geometries to a GeometryGroup.
            Dim myGeometryGroup As New GeometryGroup()
            myGeometryGroup.Children.Add(myLineGeometry)
            myGeometryGroup.Children.Add(myEllipseGeometry)
            myGeometryGroup.Children.Add(myRectGeometry)

            myPath.Data = myGeometryGroup

            ' Add path shape to the UI.
            Dim mainPanel As New StackPanel()
            mainPanel.Children.Add(myPath)
            Me.Content = mainPanel
// Create a Path to be drawn to the screen.
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
myPath.Fill = mySolidColorBrush;

// Create the line geometry to add to the Path
LineGeometry myLineGeometry = new LineGeometry();
myLineGeometry.StartPoint = new Point(10, 10);
myLineGeometry.EndPoint = new Point(50, 30);

// Create the ellipse geometry to add to the Path
EllipseGeometry myEllipseGeometry = new EllipseGeometry();
myEllipseGeometry.Center = new Point(40, 70);
myEllipseGeometry.RadiusX = 30;
myEllipseGeometry.RadiusY = 30;

// Create a rectangle geometry to add to the Path
RectangleGeometry myRectGeometry = new RectangleGeometry();
myRectGeometry.Rect = new Rect(30, 55, 100, 30);

// Add all the geometries to a GeometryGroup.
GeometryGroup myGeometryGroup = new GeometryGroup();
myGeometryGroup.Children.Add(myLineGeometry);
myGeometryGroup.Children.Add(myEllipseGeometry);
myGeometryGroup.Children.Add(myRectGeometry);

myPath.Data = myGeometryGroup;

// Add path shape to the UI.
StackPanel mainPanel = new StackPanel();
mainPanel.Children.Add(myPath);
this.Content = mainPanel;

下圖顯示在上述範例中建立的圖案。

複合幾何

使用 GeometryGroup 建立的複合幾何

您可以使用 PathGeometry 建立較複雜的圖案 (例如多邊形和具有弧形片段的圖案)。 如需如何使用 PathGeometry 建立圖案的範例,請參閱 HOW TO:使用 PathGeometry 建立圖案。 雖然本範例使用 Path 項目將圖案呈現到螢幕上,但是您也可以使用 Geometry 物件描述 GeometryDrawingDrawingContext 的內容。 這些物件也可用來裁剪 (Clipping) 和進行點擊測試。

這個範例是完整範例的一部分。如需完整範例,請參閱幾何範例 (英文)。