方法 : 複合図形を作成する
更新 : 2007 年 11 月
この例では、Geometry オブジェクトを使用して複合図形を作成し、Path 要素を使用してこれらを表示する方法を示します。次の例は、LineGeometry、EllipseGeometry、および RectangleGeometry を、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.
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;
前の例で作成した図を次に示します。
複合ジオメトリ
多角形や曲線のセグメントを持つ図形など、より複雑な図形は、PathGeometry を使用して作成できます。PathGeometry を使用して図形を作成する方法の例については、「方法 : PathGeometry を使用して図形を作成する」を参照してください。この例では Path 要素を使用して画面に図形をレンダリングしますが、Geometry オブジェクトも、GeometryDrawing または DrawingContext のコンテンツの記述に使用することができます。またこれらは、クリッピングとヒット テストに使用することもできます。
この例は、より大きなサンプルの一部分です。サンプル全体については「ジオメトリのサンプル」を参照してください。