如何在WPF中创建分成相等部分的圆并使用GeometryDrawing在每个部分中编写文本

时间:2019-06-13 22:49:41

标签: c# wpf xaml geometry geometrydrawing

我有一个动态字符串列表,长度为“ n”,需要画一个圆,将其分成相等数量的“ n”个部分,并按部分分配名称。

我发现这段Xaml代码可以生成3个部分,但我不知道如何以编程方式进行操作

<Image Width="500" Height="500" Name="image1">
            <Image.Source>
                <DrawingImage>
                    <DrawingImage.Drawing>
                        <DrawingGroup>

                            <GeometryDrawing Brush="Red">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Black" />
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Geometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,100">
                                            <PathFigure.Segments>
                                                <LineSegment Point="100,0"/>
                                                <ArcSegment Point="186.6,150"  SweepDirection="Clockwise" Size="100,100"/>
                                                <LineSegment Point="100,100"/>
                                            </PathFigure.Segments>
                                        </PathFigure>
                                    </PathGeometry>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                            <GeometryDrawing Brush="Blue">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Black"/>
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Geometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,100">
                                            <PathFigure.Segments>
                                                <LineSegment Point="186.6,150"/>
                                                <ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/>
                                                <LineSegment Point="100,100"/>
                                            </PathFigure.Segments>
                                        </PathFigure>
                                    </PathGeometry>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                            <GeometryDrawing Brush="Green">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Black"/>
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Geometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,100">
                                            <PathFigure.Segments>
                                                <LineSegment Point="13.4,150"/>
                                                <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/>
                                                <LineSegment Point="100,100"/>
                                            </PathFigure.Segments>
                                        </PathFigure>
                                    </PathGeometry>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                        </DrawingGroup>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Image.Source>
        </Image>

这是一个好方法吗?如果没有,产生我所需的最佳方法是什么?

真正令我困惑的是如何计算ArcSegment值?

            DrawingGroup drawingGroup = new DrawingGroup();
            var lastPoint = new Point(100, 0);
            for (int i = 0; i < 3; i++)
            {
                GeometryDrawing drawing = new GeometryDrawing();
                drawing.Brush = (Brush)new BrushConverter().ConvertFrom("#FF0000"); // TODO: change color

                drawing.Pen = new Pen
                {
                    Brush = (Brush)new BrushConverter().ConvertFrom("#000000")
                };

                PathSegment lineSegment1 = new LineSegment(lastPoint, true);
                lastPoint = new Point(200, 100); // TODO: calculate
                PathSegment arcSegment = new ArcSegment(lastPoint, new Size(100, 100), 0, false, SweepDirection.Clockwise, true);
                PathSegment lineSegment2 = new LineSegment(new Point(100, 100), true);
                PathFigure figure = new PathFigure(new Point(100, 100), new PathSegment[] { lineSegment1, arcSegment, lineSegment2 }, false);
                drawing.Geometry = new PathGeometry(new PathFigure[] { figure });

                drawingGroup.Children.Add(drawing);
            }

谢谢。

0 个答案:

没有答案