如何在C#/ WPF中的一行上有箭头符号?

Moo*_*oon 3 wpf

我是C#/ WPF应用程序中的图形新手.

我有一个WPF应用程序,并使用Canvas在鼠标的帮助下在运行时绘制各种对象.我在绘制带箭头的线时面临问题(如下所示):

(A)------------> ----(B)

在此箭头中,标志应位于线的第3部分(并且应始终指向移动的鼠标).例如,如果我在点"A"处单击鼠标并向点"B"移动,则箭头符号应指向"B",如上所示.

任何帮助将受到高度赞赏.

最诚挚的问候,月亮

Moo*_*oon 8

感谢您的帮助和支持.我已经解决了我的问题如下::

private static Shape DrawLinkArrow(Point p1, Point p2)
{
    GeometryGroup lineGroup = new GeometryGroup();
    double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;

    PathGeometry pathGeometry = new PathGeometry();
    PathFigure pathFigure = new PathFigure();
    Point p = new Point(p1.X + ((p2.X - p1.X) / 1.35), p1.Y + ((p2.Y - p1.Y) / 1.35));
    pathFigure.StartPoint = p;

    Point lpoint = new Point(p.X + 6, p.Y + 15);
    Point rpoint = new Point(p.X - 6, p.Y + 15);
    LineSegment seg1 = new LineSegment();
    seg1.Point = lpoint;
    pathFigure.Segments.Add(seg1);

    LineSegment seg2 = new LineSegment();
    seg2.Point = rpoint;
    pathFigure.Segments.Add(seg2);

    LineSegment seg3 = new LineSegment();
    seg3.Point = p;
    pathFigure.Segments.Add(seg3);

    pathGeometry.Figures.Add(pathFigure);
    RotateTransform transform = new RotateTransform();
    transform.Angle = theta + 90;
    transform.CenterX = p.X;
    transform.CenterY = p.Y;
    pathGeometry.Transform = transform;
    lineGroup.Children.Add(pathGeometry);

    LineGeometry connectorGeometry = new LineGeometry();
    connectorGeometry.StartPoint = p1;
    connectorGeometry.EndPoint = p2;
    lineGroup.Children.Add(connectorGeometry);
    System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
    path.Data = lineGroup;
    path.StrokeThickness = 2;
    path.Stroke = path.Fill = Brushes.Black;

    return path;
}
Run Code Online (Sandbox Code Playgroud)

谢谢,

月亮

  • 这个答案对我有帮助!对于任何想要在行尾有箭头的人,请将 1.35 更改为 1。谢谢@Moon (3认同)