C# 在WPF中绘制圆

C# 在WPF中绘制圆,c#,wpf,shape,adorner,C#,Wpf,Shape,Adorner,我正在尝试编写一个WPF应用程序,您可以通过双击窗口在窗口上画圆圈。到目前为止,我有以下代码: public class ShapeAdorner : Adorner { private readonly Ellipse _circle; public ShapeAdorner(UIElement adornedElement, Point circleCenter) : base(adornedElement) { _circle =

我正在尝试编写一个WPF应用程序,您可以通过双击窗口在窗口上画圆圈。到目前为止,我有以下代码:

public class ShapeAdorner : Adorner
{
    private readonly Ellipse _circle;

    public ShapeAdorner(UIElement adornedElement, Point circleCenter)
        : base(adornedElement)
    {
        _circle = new Ellipse
        {
            Width = 10,
            Height = 10,
            Stroke = Brushes.Black,
            StrokeThickness = 1.5
        };
        _circle.Margin =
            new Thickness(left: circleCenter.X, top: circleCenter.Y, right: 0, bottom: 0);
        base.AddVisualChild(_circle);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        _circle.Arrange(new Rect(finalSize));
        return finalSize;
    }

    protected override Size MeasureOverride(Size constraint)
    {
        _circle.Measure(constraint);
        return constraint;
    }

    protected override Visual GetVisualChild(int index)
    {
        return _circle;
    }

    protected override int VisualChildrenCount
    {
        get { return 1; }
    }
}
以下是客户端代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(myLabel);
        adornerLayer.Add(new ShapeAdorner(adornedElement: myLabel, circleCenter: e.GetPosition(myLabel)));
    } 
}

这些圆应该位于双击窗口的点的中心;但是,上面代码所画的圆圈在“双击点”的下方和右侧居中。如何解决这个问题

编辑:
myLabel
具有
Height=350
Width=525
。假设我双击点
(X,Y)
;然后在
((350+X)/2,(525+Y)/2)
处绘制圆

编辑2:为了完整起见,这里是.xaml文件:

<Window x:Class="Adorners.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Adorners project" Height="350" Width="525" MouseDoubleClick="Window_MouseDoubleClick">
    <Grid>
        <Label Name="myLabel" Content="my label" Background="Red"></Label>
    </Grid>
</Window>

您需要将圆的宽度/高度偏移一半。此处采用硬编码,便于操作:

AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(myLabel);
var point = e.GetPosition(myLabel);
point.X -= 5;
point.Y -= 5;
adornerLayer.Add(new ShapeAdorner(myLabel, point));

设置边距时,需要考虑到宽度和高度。顶部应为中心位置减去高度的一半,左侧相同:

new Thickness(
     left: circleCenter.X + (_circle.Width/2), //farther to the right
     top: circleCenter.Y - (_circle.Height/2), //higher up
     right: 0, bottom: 0);

在设置边距时,必须从顶部和左侧属性中减去半径以偏移圆。

前面的答案是正确的。然而,主要的问题是我忽略了这两行:


_circle.HorizontalAlignment=HorizontalAlignment.Left
_circle.VerticalAlignment=VerticalAlignment.Top;


默认值
Stretch
导致了一个巨大的偏移错误。

它真的离左边太远了吗?如果不补偿圆的尺寸,则它应该偏右。如果它真的离右边很远,你应该在两个维度上使用减法,否则就使用我的Awnseri我的错误-我已将其更正为“右”。感谢Kent Boogaart,H.B.和aL3891的回答。然而,我认为你还没有完全“搞定”。圆不被常数偏移;详细信息请参见我问题的“编辑”部分。事实证明,它实际上是向右偏远,而不是向左偏远。所以减法应该在两个维度上都使用,就像在每个人身上一样