Compartilhar via


Como: Create a Composite Shape

Este exemplo mostra como criar formas compostas utilizando objetos Geometry e exibindo-os utilizando um elemento Path. No exemplo a seguir, um LineGeometry, EllipseGeometry e um RectangleGeometry são utilizados com um GeometryGroup para criar uma forma composta. As geometrias são então desenhadas utilizando um elemento Path.

Exemplo

<!-- 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.
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;

A ilustração a seguir mostra a forma criada no exemplo anterior.

Geometria Composta

Uma geometria composta criada com um GeometryGroup

Formas mais complexas, tais como polígonos e formas com segmentos curvos, podem ser criadas utilizando um PathGeometry. Para um exemplo mostrando como criar uma forma utilizando um PathGeometry, veja Como: Criar um Shape usando um PathGeometry. Embora este exemplo renderize uma forma na tela utilizando um elemento Path, objetos Geometry também podem ser utilizados para descrever os conetúdo de um GeometryDrawing ou um DrawingContext. Também podem ser utilizados para clipping e testes de hit.

Este exemplo é parte de um exemplo maior; para o exemplo completo, veja Exemplo de geometrias.