开发者

How do I draw a directed line in .Net?

开发者 https://www.devze.com 2023-01-26 00:35 出处:网络
Any suggestions on how to draw a directed line in .Net? I\'m currently using Graphics.DrawLine with a Pen with dashes. But the dashes point in both directions. I want them to all point in a single dir

Any suggestions on how to draw a directed line in .Net? I'm currently using Graphics.DrawLine with a Pen with dashes. But the dashes point in both directions. I want them to all point in a single direction.

using (var p = new Pen(Color.FromArgb(190, Color.Gre开发者_运维百科en), 5))
{
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Dash;
    p.DashCap = DashCap.Triangle;
    e.Graphics.DrawLine(p, startPt, pt);
}


This might not be very helpful, but the way that we accomplished this type of thing on other projects that I have been involved with is to implement a custom "pen" that accepts the line to be drawn, calculates the actual dashes, and then draws each dash as an individual line with a "real" Pen. If you were to do something like this, then you could actually apply a "pen" level StartCap and/or EndCap to each dash.

The custom "pen" would have to accept an array or sequence of dash/gap lengths (or maybe an enumeration of standard pen styles that would be interpreted internally as specific dash/gap lengths).

We did this many years ago with a GDI-based drawing system where we had to support more line styles than the built in line styles that GDI supported.

It is an interesting exercise, but it is also probably more trouble than it is worth unless you REALLY have to have the dashes drawn as you describe.

[EDIT]

Here is some sample code for how you might do this, if you are so inclined.

First is an extension method on the Graphics object. It takes a pen, dash length, gap length, start point, and end point. It interpolates along the line p1->p2, drawing a "dash" length segment and then skipping a "gap" length segment. The passed-in pen should be solid with an arrow endcap (to achieve the effect you were asking for).

The code is pretty rough, but the behavior is, more or less, like this: If the input line is shorter then the total dash+gap length, then the line is drawn as-is using the input pen. If the input line is longer than the total dash+gap length, then "dash" length lines are drawn until the "remainder" of the line is less than the dash+gap length, at which point the remainder is drawn as-is with the input pen.

If you wanted to implement Path drawing, then you would have to interpolate around the intermediate vertices (unless you wanted to cheap out and just compute dashes for each segment indepdently).

    public static void DrawDashedLine(this Graphics g, Pen p, float dash, float gap, PointF s, PointF e)
    {
      float dx = e.X - s.X;
      float dy = e.Y - s.Y;

      float len = (float)Math.Sqrt(dx * dx + dy * dy);
      float remainder = len;

      float vx = dx / len;
      float vy = dy / len;

      if (len <= dash + gap)
      {
        g.DrawLine(p, s, e);
        return;
      }

      PointF last = s;

      while (remainder > dash + gap)
      {
        PointF p1 = new PointF(last.X, last.Y);
        PointF p2 = new PointF(p1.X + vx*dash, p1.Y + vy*dash);

        g.DrawLine(p, p1, p2);

        last = new PointF(p2.X + vx*gap, p2.X + vy*gap);

        remainder = remainder - dash - gap;
      }

      if (remainder > 0)
      {
        g.DrawLine(p, last, e);
      }
    }
  }

Here is how you would call it:

private void button1_Click(object sender, EventArgs e)
{
  using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
  {
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Solid;
    var graph = this.CreateGraphics();
    graph.DrawDashedLine(p, 20, 10, new PointF(20, 20), new PointF(500, 500));
  } 
}

I don't claim that the code is great (or even necessarily good ;-), but it should give you a good starting point if you are really interested in drawing your directed line.

Good luck!


Hmm... maybe I am missing something, but when I use this (almost) exact same code, I get a directed arrow with only one point.

Here is my code:

private void button2_Click(object sender, EventArgs e)
{
    using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
    {
        p.StartCap = LineCap.Round;
        p.EndCap = LineCap.ArrowAnchor;
        p.CustomEndCap = new AdjustableArrowCap(3, 3);
        p.DashStyle = DashStyle.Dash;
        p.DashCap = DashCap.Triangle;
        var graph = this.CreateGraphics();
        graph.DrawLine(p, new Point(20, 20), new Point(150, 150));
    }
}

And the output:

How do I draw a directed line in .Net?

0

精彩评论

暂无评论...
验证码 换一张
取 消