Zeichnen Sie eine Linie von einem Punkt zu Punkt (Point to Point) in Form MouseDown

Ich arbeite seit Tagen an einem Programm, dass zeichnet einen raster mit Punkten, und ich musste wieder mehrere Male wegen schlechter Ansatz /zu kompliziert. Ich habe zu einem Punkt kommen, jetzt, wo ich zeichnen Sie eine Linie von einem angeklickten Punkt (point) zu einem zweiten angeklickten Punkt (point) in die form.
Im ernst ich habe schon stundenlang sogar tagelang meine Zeit mit der Suche für den richtigen Ansatz.
Für jetzt habe ich nur geschafft, eine Linie von einem Punkt auf dem Formular an eine andere Stelle auf dem Formular, auf zufällige Klicks...
Könnte mir bitte jemand helfen, den code getan, es ist nur frustrierend mich, wie ich es habe keine Fortschritte, nachdem alle versuche, Sie zeichnen ein raster mit Punkten..
Also, was ich tun möchte, ist "zeichne eine Linie von einem angeklickten Punkt (point) zu einem zweiten angeklickten Punkt (point) in der form".

Siehe unten mein code:

Form1.cs:

public partial class Form1 : Form
{
    private GridDrawing drawing;

    private Point point1;
    private Point point2;
    List<Point> p1List = new List<Point>(); //Temp
    List<Point> p2List = new List<Point>(); //Temp

    //True if point1 must be updated
    //False if point2 must be updated
    private bool firstPoint = true;

    private int sizeOfDot;
    private int rows;
    private int columns;

    public Form1()
    {
        InitializeComponent();
        sizeOfDot = 10;     //The size of the dot
        rows = 6;           //The amount of rows for the matrix
        columns = 8;        //The amount of columns for the matrix
    }

    private void Form_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.White, ClientRectangle);   //Fill the form in white
        drawing = new GridDrawing(this, rows, columns);             //Control, Rows, Columns

        foreach (var piece in drawing.Pieces)                           //Draws all the dots
        {
            e.Graphics.FillEllipse(Brushes.Black, (piece.Dot.X - sizeOfDot / 2),
                (piece.Dot.Y - sizeOfDot / 2), sizeOfDot, sizeOfDot);   //Draws the dot
        }

        using (var pen = new Pen(Color.Black, 2))
        {
            for (int i = 0; i < p1List.Count; i++)
            {
                e.Graphics.DrawLine(pen, p1List[i], p2List[i]);
            }
        }
    }

    private void startToolStripMenuItem_Click(object sender, EventArgs e)
    {}

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (firstPoint)             //Update point1 or point2
        {
            //Point 1
            point1.X = e.X;
            point1.Y = e.Y;
        }
        else
        {
            //Point 2
            point2.X = e.X;
            point2.Y = e.Y;

            p1List.Add(point1);
            p2List.Add(point2);
        }
        firstPoint = !firstPoint;   //Change the bool value
        Invalidate();               //Redraw
    }

    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        Invalidate();
    }
}

GridDrawing.cs:

public class GridDrawing
{
    private int columns;
    private int rows;
    private List<GridPiece> pieces;
    private Point dot;

    //private Point point1; //point1 to start drawing line from
    //private Point point2; //point2 to end drawing line from

    ///<summary>
    ///Constructs a grid
    ///</summary>
    ///<param name="ctrl"></param>
    ///<param name="rows"></param>
    ///<param name="columns"></param>
    ///<param name="sizeOfDot"></param>
    public GridDrawing(Control ctrl, int rows, int columns)
    {
        this.rows = rows;               //The amount of rows in the matrix.
        this.columns = columns;         //The amount of columns in the matrix.

        this.pieces = new List<GridPiece>();                //Initializes the List GridPieces

        int xOffset = (int)ctrl.ClientRectangle.Width / (columns + 1);  //FP offset for X
        int yOffset = (int)ctrl.ClientRectangle.Height / (rows + 1);    //FP offset for Y

        //Generate the dots
        for (int i = 0; i < rows; i++)          //Matrix with 6 rows                      
        {
            for (int j = 0; j < columns; j++)   //Matrix with 8 columns
            {
                dot = new Point((j + 1) * xOffset, (i + 1) * yOffset);  //Center of the dot
                GridPiece p = new GridPiece(dot);  //Creates a piece
                pieces.Add(p);                     //Puts the piece that has to be drawn in the List<GridPiece>pieces
            }
        }
    }

    public List<GridPiece> Pieces   //Property List<GridPiece>pieces
    {
        get { return this.pieces; }
    }

    public Point Dot                //Property Point dot
    {
        get { return this.dot; }
    }
}

GridPiece.cs:

public class GridPiece
{
    private Point dot;

    ///<summary>
    ///Constructor of GriedPiece
    ///</summary>
    ///<param name="bmpPic"></param>
    ///<param name="position"></param>
    public GridPiece(Point dot)
    {
        this.dot = dot;
    }

    public Point Dot
    {
        get { return dot; }
    }
}

Hier ist ein Beispiel, wie ich versuche, es so Aussehen wie
Zeichnen Sie eine Linie von einem Punkt zu Punkt (Point to Point) in Form MouseDown

Könnte mir bitte jemand helfen?

Einfach nur neugierig, ist es eine Hausaufgabe?
Vielen Dank für das Lesen Anders, und es ist ein kleiner Teil eines Projektes.

InformationsquelleAutor Konni | 2011-04-09

Schreibe einen Kommentar