Partager via


Comment : créer une forme composite

Mise à jour : novembre 2007

Cet exemple montre comment créer des formes composites à l'aide d'objets Geometry et les afficher à l'aide d'un élément Path. Dans l'exemple suivant, une LineGeometry, une EllipseGeometry et un RectangleGeometry sont utilisés avec un GeometryGroup pour créer une forme composite. Les géométries sont ensuite dessinées à l'aide d'un élément Path.

Exemple

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

L'illustration suivante montre la forme créée dans l'exemple précédent.

Géométrie composite

Géométrie composée créée à l'aide de GeometryGroup

Il est possible de créer des formes plus complexes, telles que des polygones et des formes intégrant des segments courbés, à l'aide d'un PathGeometry. Pour obtenir un exemple montrant comment créer une forme à l'aide d'un PathGeometry, consultez Comment : créer une forme à l'aide d'un PathGeometry. Bien que cet exemple restitue une forme à l'écran à l'aide d'un élément Path, des objets Geometry peuvent également être utilisés pour décrire le contenu d'un GeometryDrawing ou d'un DrawingContext. Ils peuvent également être utilisés pour des découpages et des tests d'atteinte.

Cet exemple est extrait d'un exemple plus complet ; pour y accéder, consultez Géométries, exemple.