Comment : créer un LineSegment dans un PathGeometry

Cet exemple indique comment créer un segment de ligne. Pour se faire, utilisez les classes PathGeometry, PathFigure et LineSegment.

Exemple

Les exemples suivants dessinent un LineSegment de (10, 50) à (200, 70). L'illustration suivante présente le LineSegment obtenu ; un arrière-plan de grille a été ajouté pour afficher le système de coordonnées.

LineSegment dessiné de (10,50) à (200,700)

LineSegment dans un PathFigure

[xaml]

En langage Extensible Application Markup Language (XAML), vous pouvez utiliser la syntaxe d'attribut pour décrire un chemin d'accès.

<Path Stroke="Black" StrokeThickness="1"  
  Data="M 10,50 L 200,70" />

[xaml]

(Notez que cette syntaxe d'attribut crée en fait un StreamGeometry, version allégée d'un PathGeometry. Pour plus d'informations, consultez la page Syntaxe XAML pour les tracés.)

En langage XAML, vous pouvez également dessiner un segment de ligne à l'aide de la syntaxe d'élément objet. L'exemple suivant est équivalent à l'exemple XAML précédent.

            Dim myPathFigure As New PathFigure()
            myPathFigure.StartPoint = New Point(10, 50)

            Dim myLineSegment As New LineSegment()
            myLineSegment.Point = New Point(200, 70)

            Dim myPathSegmentCollection As New PathSegmentCollection()
            myPathSegmentCollection.Add(myLineSegment)

            myPathFigure.Segments = myPathSegmentCollection

            Dim myPathFigureCollection As New PathFigureCollection()
            myPathFigureCollection.Add(myPathFigure)

            Dim myPathGeometry As New PathGeometry()
            myPathGeometry.Figures = myPathFigureCollection

            Dim myPath As New Path()
            myPath.Stroke = Brushes.Black
            myPath.StrokeThickness = 1
            myPath.Data = myPathGeometry
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);

LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);

PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>

Cet exemple est extrait d'un exemple plus complet ; pour l'obtenir, consultez Géométries, exemple.

Voir aussi

Référence

PathFigure

PathGeometry

GeometryDrawing

Path

Concepts

Vue d'ensemble de Geometry