5

I need to be able to draw a polygon using mouse click locations. Here is my current code:

 //the drawshape varible is called when a button is pressed to select use of this tool
             if (DrawShape == 4)
                {
                    Point[] pp = new Point[3];
                    pp[0] = new Point(e.Location.X, e.Location.Y);
                    pp[1] = new Point(e.Location.X, e.Location.Y);
                    pp[2] = new Point(e.Location.X, e.Location.Y);
                    Graphics G = this.CreateGraphics();
                    G.DrawPolygon(Pens.Black, pp);
                }

Thanks

Henrik
  • 22,652
  • 6
  • 38
  • 90
Chris Bacon
  • 997
  • 8
  • 28
  • 42
  • I assume you are on winforms. You provided code, but does it work? What is the your question? – GôTô Oct 21 '10 at 14:00
  • Yes i am, and yeah it doesn't work, i can;t work out how to store mouseclicks in the array for them to be joined up by a line, as if in MS Paint – Chris Bacon Oct 21 '10 at 14:02
  • How should a user draw a polygon? Line by line, or the entire polygon at once? You want the user to left-click x times for the points and then right-click to draw (else how do you know when the user has finished)? – GôTô Oct 21 '10 at 14:18
  • Ideally line by line with a unlimted amount of points until the user right clicks to stop the polygon shape being drawn – Chris Bacon Oct 21 '10 at 14:25

2 Answers2

4

Ok here is some sample code:

private List<Point> polygonPoints = new List<Point>();

private void TestForm_MouseClick(object sender, MouseEventArgs e)
{
    switch(e.Button)
    {
        case MouseButtons.Left:
            //draw line
            polygonPoints.Add(new Point(e.X, e.Y));
            if (polygonPoints.Count > 1)
            {
                //draw line
                this.DrawLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]);
            }
            break;

        case MouseButtons.Right:
            //finish polygon
            if (polygonPoints.Count > 2)
            {
                //draw last line
                this.DrawLine(polygonPoints[polygonPoints.Count - 1], polygonPoints[0]);
                polygonPoints.Clear();
            }
            break;
    }
}

private void DrawLine(Point p1, Point p2)
{
    Graphics G = this.CreateGraphics();
    G.DrawLine(Pens.Black, p1, p2);
}
GôTô
  • 7,771
  • 3
  • 30
  • 42
3

First, add this code:

List<Point> points = new List<Point>();

On the object you are drawing on, capture the OnClick event. One of the arguments should have the X and Y coordinates of the click. Add them to the points array:

points.Add(new Point(xPos, yPos));

And then finally, where you're drawing the lines, use this code:

 if (DrawShape == 4)
 {
     Graphics G = this.CreateGraphics();
     G.DrawPolygon(Pens.Black, points.ToArray());
 }

EDIT:

Ok, so the above code isn't exactly correct. First of all, its most likely a Click event instead of a OnClick event. Second, To get the mouse position, you need two variables declared up with the points array,

    int x = 0, y = 0;

Then have a mouse move event:

    private void MouseMove(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
    }

Then, in your Click event:

    private void Click(object sender, EventArgs e)
    {
        points.Add(new Point(x, y));
    }
Entity
  • 7,263
  • 19
  • 69
  • 117