结合LineGeometry和EllipseGeometry(在代码中,而不是XAML中)

本文关键字:XAML 代码 LineGeometry EllipseGeometry 结合 | 更新日期: 2023-09-27 18:12:22

我想用WPF创建一个自定义形状。对于初学者来说,我只是想创建一个简单的线,在每一端都有一个圆(我知道有LineCaps,但这不是我想要的)。

我看过一些教程,最简单的方法是使用CombinedGeometry。然而,我不能让它正常工作。下面是我创建几何对象的代码:

protected override Geometry DefiningGeometry
    {
        get
        {
            Point ellipseCenter1 = new Point(X1 - this.CapDiameter / 2, Y1 - this.CapDiameter / 2);
            Point ellipseCenter2 = new Point(X2 - this.CapDiameter / 2, Y2 - this.CapDiameter / 2);
            var ellipse1 = new EllipseGeometry(ellipseCenter1, CapDiameter, CapDiameter);
            var ellipse2 = new EllipseGeometry(ellipseCenter2, CapDiameter, CapDiameter);
            var line = new LineGeometry(this.StartPoint, this.EndPoint);
            var combined1 = new CombinedGeometry(GeometryCombineMode.Union, ellipse1, line);
            var combined2 = new CombinedGeometry(GeometryCombineMode.Union, combined1, ellipse2);
            // Freeze the geometry for performance benefits
            combined2.Freeze();
            return combined2;
        }
    }

然而,由于某种原因,这并没有划清界限。它画了两个圆,但没有画直线。很明显我遗漏了什么,有人能指出来吗?:)

为了以防万一,下面是类的其余部分:

#region Dependency Properties
public static readonly DependencyProperty X1Property = 
    DependencyProperty.Register(
        "X1", 
        typeof(double),
        typeof(CappedLine), 
        new FrameworkPropertyMetadata(
                0.0, 
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty Y1Property =
    DependencyProperty.Register(
        "Y1",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty X2Property =
    DependencyProperty.Register(
        "X2",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty Y2Property =
    DependencyProperty.Register(
        "Y2",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
public static readonly DependencyProperty CapDiameterProperty =
    DependencyProperty.Register(
        "CapDiameter",
        typeof(double),
        typeof(CappedLine),
        new FrameworkPropertyMetadata(
                0.0,
                FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
            )
    );
#endregion
#region CLR Properties
public double X1
{
    get { return (double)base.GetValue(X1Property); }
    set { base.SetValue(X1Property, value); }
}
public double Y1
{
    get { return (double)base.GetValue(Y1Property); }
    set { base.SetValue(Y1Property, value); }
}
public double X2
{
    get { return (double)base.GetValue(X2Property); }
    set { base.SetValue(X2Property, value); }
}
public double Y2
{
    get { return (double)base.GetValue(Y2Property); }
    set { base.SetValue(Y2Property, value); }
}
public Point StartPoint
{
    get { return (new Point(X1, Y1)); }
}
public Point EndPoint
{
    get { return (new Point(X2, Y2)); }
}
public double CapDiameter
{
    get { return (double)base.GetValue(CapDiameterProperty); }
    set { base.SetValue(CapDiameterProperty, value); }
}
#endregion

这里是XAML代码我用来测试它:

<Window x:Class="HG.WPF.Shapes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:shapes="clr-namespace:Tomers.WPF.Shapes"
    Title="WPF Custom Shapes" Height="300" Width="300" ResizeMode="NoResize">
    <Canvas Background="Black">
        <shapes:CappedLine X1="30" Y1="30" X2="200" Y2="200" CapDiameter="5" Fill="White" Stroke="White" StrokeThickness="2"></shapes:CappedLine>        
    </Canvas>
</Window>

结合LineGeometry和EllipseGeometry(在代码中,而不是XAML中)

你的LineGeometry对象将消失,如果你试图Union他们。从MSDN:

GeometryCombineMode属性指定了这两个几何图形将如何结合。注意,CombinedGeometry组合由两个几何图形,也就是没有面积的几何图形(比如LineGeometry)组合后消失

您可以使用GeometryGroup:

GeometryGroup combined = new GeometryGroup();
combined.Children.Add(ellipse1);
combined.Children.Add(ellipse2);
combined.Children.Add(line);

或者您可以先在椭圆对象上使用CombinedGeometry,然后使用GeometryGroup将其与线条组合在一起。