4

I am trying to create connector symbols like in visio. I have created those connectors by using Graphics.Drawline method. But I don't know how to make bends to smooth curves like in Microsoft visio.

Code:

protected override void Render(Graphics gfx)
{
    PointF[] pts = GetPathPoints();
    gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    float x = (float)((pts[0].X + pts[1].X) / 2 - 5);
    float y = 0F;

    PointF start = new PointF((float)(pts[0].X), (float)(pts[0].Y));
    PointF end = new PointF((float)(pts[1].X), (float)(pts[1].Y));

    PointF pt1 = new PointF((float)(start.X), (float)(start.Y-50));
    PointF pt2 = new PointF((float)(end.X), (float)(end.Y - 50));

    using (Pen pen = this.LineStyle.CreatePen())
    {
        gfx.DrawLine(pen, start, pt1);
        gfx.DrawLine(pen, pt1, pt2);
        gfx.DrawLine(pen, pt2, end);
    }
}

PLease see the connectors that have smooth bends in the below link: http://en.wikipedia.org/wiki/File:BPMN-CollectVotes.jpg

How can I draw lines with rounded corner lines like in visio?

Justin
  • 84,773
  • 49
  • 224
  • 367
Amsath
  • 81
  • 1
  • 7
  • Not sure if there is an easier way, there probably is, but youcan always undertake a first attempt using quarter circles for the curved bends. – Anton Sep 08 '11 at 06:03
  • 1
    You need to detach two line ends that connect to one same point (corner), pull them back 20 pixels (more or less) and then draw an arc connecting those two line ends. There is no other way. – Cipi Sep 08 '11 at 08:57
  • learner, you just need to read this first: http://stackoverflow.com/questions/1805582/net-gdi-drawing-lines-with-rounded-corners – Davide Piras Sep 08 '11 at 11:34
  • Davide, thanks for your link....It gives some clues to go ahead in my project...................... – Amsath Sep 09 '11 at 11:33

2 Answers2

0

Use line caps.

pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;

https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-draw-a-line-with-line-caps

Tanner Ornelas
  • 602
  • 1
  • 8
  • 17
0

You would need to use a GraphicsPath object. Also be prepared to lose some hair :)

Look at the PaintLineHighlight method @ http://xacc.svn.sourceforge.net/viewvc/xacc/xacc/Drawing/Utils.cs?revision=90&view=markup

xpda
  • 15,585
  • 8
  • 51
  • 82
leppie
  • 115,091
  • 17
  • 196
  • 297