WPF中的填充弧

时间:2011-05-23 19:17:03

标签: c# wpf geometry

我想绘制一个像这样的数字:Concentric arcs

我需要为每个弧段设置一个独特的元素,我可以根据需要处理事件和重新着色。我对如何在WPF中创建适当的几何图形有点不确定。我可以从圆的半径和中心的角度轻松计算每个弧段的四个点。外半圆的半径为100,内圈的半径为50,红色的四个点(从左上角顺时针,原点在圆的顶部):

0,0
70,30
35,65
0,50

使用这些点我创建了一个绘制线段的简单路径:

<Path Stroke="Black" Fill="Black" StrokeThickness="1" >
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigure StartPoint="0,0">
          <PathFigure.Segments>
            <ArcSegment Point="70,30" />
            <LineSegment Point="35,65" />
            <ArcSegment Point="0,50" />
          </PathFigure.Segments>
        </PathFigure>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

但是这只画了一条直线的梯形。我知道我可以改变ArcSegments上的尺寸,但我似乎无法弄清楚它是如何影响曲率的。我希望弧线跟随主圈,但我不知道如何表达。如何使圆弧具有正确的曲率?

另外,如何在c#代码后面而不是在xaml中表达和添加路径?

3 个答案:

答案 0 :(得分:5)

我画的确是那种形状(两个同轴圆弧和两个连接它们的放射线),如下所示:

new LineSegment(new Point(x2, y2), true),
new ArcSegment(new Point(x3,y3),new Size(100*outerRadius,100*outerRadius), 0,largeAngle, SweepDirection.Clockwise, true),
new LineSegment(new Point(x4, y4), true),
new ArcSegment(new Point(x1, y1),new Size(100*innerRadius,100*innerRadius), 0,largeAngle, SweepDirection.Counterclockwise, true),

显然这是代码而不是XAML,但它可能会给你一个启动。

答案 1 :(得分:1)

XAML 代码

<Window x:Class="PopupTargetElement.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:ed="http://schemas.microsoft.com/expression/2010/drawing"         
        mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">        
    <Grid>
        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="360" Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="300"
                Stretch="None" Stroke="Black" StartAngle="0" VerticalAlignment="Center" Width="300"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="360" Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="220"
            Stretch="None" Stroke="Black" StartAngle="0" VerticalAlignment="Center" Width="220"/>
        
        <Ellipse   Width="140"  Height="140" Fill="Black" Stroke="Black" StrokeThickness="1" />

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="45" Fill="Black" HorizontalAlignment="Center" Height="300" 
            Stretch="None" Stroke="Black" StartAngle="0" VerticalAlignment="Center" Width="300"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="135" Fill="Black" HorizontalAlignment="Center" Height="300"
            Stretch="None" Stroke="Black" StartAngle="90" VerticalAlignment="Center" Width="300"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel"  EndAngle="225" Fill="Black" HorizontalAlignment="Center" Height="300" 
            Stretch="None" Stroke="Black" StartAngle="180" VerticalAlignment="Center" Width="300"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel"  EndAngle="315" Fill="Black" HorizontalAlignment="Center" Height="300"
            Stretch="None" Stroke="Black" StartAngle="270" VerticalAlignment="Center" Width="300"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="90" Fill="Black" HorizontalAlignment="Center" Height="220" 
            Stretch="None" Stroke="Black" StartAngle="45" VerticalAlignment="Center" Width="220"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="180" Fill="Black" HorizontalAlignment="Center" Height="220" 
                Stretch="None" Stroke="Black" StartAngle="135" VerticalAlignment="Center" Width="220"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="270" Fill="Black" HorizontalAlignment="Center" Height="220"
            Stretch="None" Stroke="Black" StartAngle="225" VerticalAlignment="Center" Width="220"/>

        <ed:Arc ArcThickness="40" ArcThicknessUnit="Pixel" EndAngle="360" Fill="Black" HorizontalAlignment="Center" Height="220"
            Stretch="None" Stroke="Black" StartAngle="315" VerticalAlignment="Center" Width="220"/>
    </Grid>
</Window>

在您的项目中添加程序集引用。 Microsoft.Expression.Drawing

对于 xmlns:ed=http://schemas.microsoft.com/expression/2010/drawing

输出

enter image description here

答案 2 :(得分:0)

还有一个选项可以将表达式2010绘图命名空间引入XAML。这样很容易。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" x:Class="Arcs.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ed:Arc ArcThickness="30"
            ArcThicknessUnit="Pixel"
            StartAngle="30"
            EndAngle="130"
            HorizontalAlignment="Left"
            Height="179" Margin="195,62,0,0"
            Stretch="None"
            Stroke="CornflowerBlue"
            Fill ="CornflowerBlue"
            VerticalAlignment="Top"
            Width="179" />
</Grid>

编辑:这将引入安装Blend时安装的“Microsoft.Expression.Drawing”。如果客户端计算机没有这个,那么这将失败。另一方面,您可以使用软件打包和重新分发dll。微软允许这样做。