0

2 つの長方形を結ぶ直線を作成するにはどうすればよいでしょうか? 現時点では、私はこれを持っています:

if (listBox1.Items.Count >= 2)
{
    e.Graphics.DrawLine(Pens.AliceBlue, new Point(/*??*/), new Point(n._x, n._y));                    
}

With the second new Point being where I placed my new Rectangle but I am not sure how to get the point of the Rectangle beforehand.

My rectangles X and Y are stored in a list like so:

public BindingList<Node> nodeList = new BindingList<Node>();

My main goal would be too add a line to each of my rectangles as they are drawn.

For example: Place one rectangle down, nothing happens, place another one down, add a line connecting the two, add a third, add a line connecting the second and third one together. But if I can get one going I can try and work out how to continuously add these lines.

Thanks for any help!

4

2 に答える 2

0

長方形のリストがある場合は、次のようにそれらを結ぶ線で長方形を描くことができます。

void drawRectangles(Graphics g, List<Rectangle> list) {
    if (list.Count == 0) {
        return;
    }

    Rectangle lastRect = list[0];
    g.DrawRectangle(Pens.Black, lastRect);

    // Indexing from the second rectangle -- the first one is already drawn!
    for (int i = 1; i < list.Count; i++) {
        Rectangle newRect = list[i];
        g.DrawLine(Pens.AliceBlue, new Point(lastRect.Right, lastRect.Bottom), new Point(newRect.Left, newRect.Top));
        g.DrawRectangle(Pens.Black, newRect);
        lastRect = newRect;
    }
}

接続するコーナーを決定するためにスマートコードを挿入することもできますが、それはあなた次第です。

于 2012-10-18T07:23:21.000 に答える
0

このコード例を必要とする可能性のある他の人のために。for ループは 0 から開始する必要があります。

for (int i = 1; i < list.Count; i++)
   {
       //Code here
   }

次のようにする必要があります。

for (int i = **0**; i < list.Count; i++)
   {
       //Code here
   }
于 2012-10-18T10:43:42.667 に答える