WPF learning note 2

2. Geometry

One of the shapes Padl uses Geometry to draw. The Geometry element can also be used elsewhere, such as DrawingBrush.
The Geometry element is very similar to the shape. Like Line, Ellipse, and Rectangle shapes, there are also
Geometry elements: lineGeometry, EllipseGeometry, and regalegeometry ". There are significant areas between shape and geometry
Farewell. Shape is a FrameworkElement that can be used as any class with UIElement as its child element.
FrameworkElement is derived from UIElement. Shapes participate in the layout of the system and present themselves. The Geometry class does not render
In itself, the features and system overhead are also less than shpae class. The Geometry class is derived from the Freezable base class and can be shared across multiple threads.
The Path class uses geometry to draw. Geometry can be set with the Data property of Path. Simple geometry that can be set up
Shape elements include ellipse geometry for drawing ellipses, lineGeometry for drawing lines, and regalegeometry for drawing rectangles.
As shown in the following example, you can merge multiple geometry by using combinedgeometry.
CombineGeometry has the Geometry1 and Geometry2 attributes, which can be combined by using combinegeometriymode,
Constitutes Union, intersect, Xor, and Exclude. Union will merge two geometries, and intersect only takes two geometries
Area covered, Xor is the opposite of intersect, showing the area covered by one geometry, but not both
Area of. Exclude shows the area where the first geometry subtracts the second geometry

The following example merges an ellipse geometry and a reclaim geometry to produce a union

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Canvas>
        <Path Canvas.Top="0" Canvas.Left="250" Fill="Blue" Stroke="Black">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <EllipseGeometry Center="80,60" RadiusX="80" RadiusY="40" />
                        
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry Rect="30,60 105 50" />
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Canvas>
</Window>

Design sketch:

You can also use segments to create geometry. The geometry class PathGeometry uses segments to draw. The following code snippet uses the BezierSegment and LINESEGMENT elements to draw a red figure and a green figure. The first Bezier segment draws a Bezier curve between the beginning (70,40), the end (150,63), the control points (90,37) and (130,46) of the graph. The following LINESEGMENT draws a line segment using the end point and (⒓ 0110) of the Bezier curve:

        <Path Canvas.Left="0" Canvas.Top="0" Fill="Red" Stroke="Blue"
              StrokeThickness="2.5">
            <Path.Data>
                <GeometryGroup>
                    <PathGeometry>
                        <PathGeometry.Figures>
                            <PathFigure StartPoint="70,40" IsClosed="True">
                                <PathFigure.Segments>
                                    <BezierSegment Point1="90,37" Point2="130,46"
                                                   Point3="150,63" />
                                    <LineSegment Point="120,110" />
                                    <BezierSegment Point1="100,95" Point2="70,90"
                                                   Point3="45,91"/>
                                    <LineSegment Point="70,40" />
                         

                                </PathFigure.Segments>
                            </PathFigure>
                        </PathGeometry.Figures>
                    </PathGeometry>
                </GeometryGroup>
            </Path.Data>
        </Path>
        <Path Canvas.Left="0" Canvas.Top="0" Fill="Green" Stroke="Blue"
              StrokeThickness="2.5">
            <Path.Data>
                <GeometryGroup>
                    <PathGeometry>
                        <PathGeometry.Figures>
                            <PathFigure StartPoint="160,70">
                                <PathFigure.Segments>
                                    <BezierSegment Point1="175,85" Point2="200,99"
                                                   Point3="215,100"/>
                                    <LineSegment Point="195,148" />
                                    <BezierSegment Point1="174,150" Point2="142,140" Point3="129,115" />
                                    <LineSegment Point="160,70"/>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathGeometry.Figures>
                    </PathGeometry>
                </GeometryGroup>
            </Path.Data>
        </Path>

Design sketch:


 

Published 7 original articles, won 0 praise and 361 visitors
Private letter follow

Keywords: less

Added by Rodders on Wed, 05 Feb 2020 07:57:22 +0200