2011-03-04 77 views
2

我是C#/ WPF应用程序中的图形新手。如何在C#/ WPF中的一行上使用箭头符号?

我有一个WPF应用程序,并使用画布在运行时借助鼠标绘制各种对象。

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

在该箭头标志:我在绘制与箭头(如下面)的线面临的问题应该在行的第三部分(并且应该始终指向移动的鼠标)。例如,如果我在点“A”点击鼠标并移向点“B”,那么箭头符号应该指向“B”,如上所示。

任何帮助将不胜感激。

最好的问候, 月亮

回答

7

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

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; 
} 

感谢,

月亮

+0

这个答案对我有帮助!对于任何想要在行尾添加箭头的人,请将1.35更改为1.谢谢@Moon – 2017-02-04 22:13:49

0

我会做这个用你想要的箭头的图像。制作一个快速的photoshop .jpg或.png用于默认的箭头图像,然后根据点A和B之间的距离缩放它。旋转稍微复杂一点,但是如果您已经采用了基本的三角形过程,它应该仍然很容易。

另一种方式,你可以去做这个是通过绘制像素。这通常要困难得多,并且在这种情况下确定箭头方向的代码更加困难。再次,我建议上面的方法。

+0

谢谢您回复。我不能使用图像选项,b'coz我必须将此图像序列化为XML文件并将其作为对象存储。 – Moon 2011-03-05 01:23:46

0

你可以使用Line类来绘制组成你箭头的线段。例如,你可以从A画到B的一条长线和两条较小的,有角度的线画出指向B的头。

找到合适的线位所涉及的数学不应该太困难。对于头部来说,只需确定两段的大小,然后将它们拉近B角30度左右。如果您有任何问题,请告知我。

图像技术的问题是头部会随着箭头的长度拉伸。您将不得不在头部和其他部分之间分割位图。

2

可以使用Canvas控制来表示的箭头,

<Canvas Margin="5" Width="60" Height="20">    
    <Line X1="10" Y1="10" X2="50" Y2="10" Stroke="Black" 
        StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/> 
    <Line X1="35" Y1="10" X2="30" Y2="5" Stroke="Black" 
        StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/> 
    <Line X1="35" Y1="10" X2="30" Y2="15" Stroke="Black" 
        StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/> 
</Canvas> 

如果你想在运行时创建这个,你可以创建一个在后面类的代码的Canvas

请注意,上面的示例中,实际上并不遵循箭头符号应该在行的第3部分规则中,您可能必须相应地修改行的坐标。

+0

嗨Devendra,谢谢你的回复。形状是正确的,但我认为我无法让你的问题清楚。问题在于画布最初应该是空的,当用户点击画布并开始拖动鼠标时,这个箭头图像应该弹出在屏幕上,用户可以在运行时决定图像的长度(意思是当他拖动此图像)。 – Moon 2011-03-05 01:22:07

3

我只画了两个与创建此的Expression Blend 4的箭头形状:

 <Window 
     ... 
     xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" ... /> 

    <ed:BlockArrow Height="8" Margin="119,181,169,0" Orientation="Left" Stroke="Black" VerticalAlignment="Top" /> 
    <ed:LineArrow Fill="#FFF4F4F5" Height="1" Margin="119,117,169,0" Stroke="#FF027602" VerticalAlignment="Top" BendAmount="0" StartCorner="TopRight" StrokeThickness="2"/> 
+1

您需要为您的投票提供解释。这可能并不总是最合适的解决方案,但除非您能证明其他方面,否则这是一个有效的选择。 – xr280xr 2014-04-04 18:06:03

相关问题