Compartir vía


Cómo: Crear una forma compuesta

En este ejemplo se muestra cómo crear formas compuestas mediante objetos Geometry y mostrarlas mediante un elemento Path. En el ejemplo siguiente se usan un LineGeometry, un EllipseGeometry y un RectangleGeometry con un GeometryGroup para crear una forma compuesta. A continuación, las geometrías se dibujan mediante un elemento Path.

Ejemplo

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

La siguiente ilustración muestra la forma creada en el ejemplo anterior.

Una geometría compuesta creada con un elemento GeometryGroup
Geometría compuesta

Se pueden crear formas más complejas, como polígonos y formas con segmentos curvados, mediante un PathGeometry. Para obtener un ejemplo en el que se muestra cómo crear una forma mediante un PathGeometry, consulte Crear una forma mediante un PathGeometry. Aunque en este ejemplo se representa una forma en la pantalla mediante un elemento Path, los objetos Geometry también se pueden usar para describir el contenido de un GeometryDrawing o un DrawingContext. También se pueden usar para hacer recortes y realizar pruebas de posicionamiento.

Este ejemplo forma parte de un ejemplo más extenso; para obtener el ejemplo completo, vea Ejemplo de geometrías.