简体   繁体   English

图纸上的零件清单

[英]Drawing list of pieces on the form

First of all I'm new to the C# world. 首先,我是C#世界的新手。 I just started this year learning about C# with no programming background experience. 我今年才开始学习C#,没有编程经验。 I came here with a problem that I would like to get solved asap. 我来到这里是想尽快解决的问题。 I managed to write a little code to create a list with Bitmaps including the draw positions. 我设法编写了一些代码,以使用位图(包括绘制位置)创建列表。 What I'm trying to do is to draw every piece (picture of a dot) in the list on the form. 我想做的是绘制表单列表中的每一块(一个点的图片)。 I've been working hours on this just trying to figure out how to get the list drawn on the form... I placed comments behind the code to make it easier for the reader to understand whats it's for so it won't cause any brain damage or sweat to the reader. 我一直在努力工作,只是想弄清楚如何在表单上绘制列表...我在代码后面放置了注释,以使读者更容易理解其用途,因此不会引起任何问题。大脑受损或出汗。 :p See below for my code: :p请参阅下面的代码:

Form1.cs Form1.cs

public partial class Form1 : Form
{
    private GridDrawing drawing;
    private Bitmap bmpPic;

    public Form1()
    {
        InitializeComponent();

        bmpPic = new Bitmap("Dot.png"); // Picture(Dot 28*28 pixels)
        this.Paint += Form_Paint;

    }

    private void Form_Paint(object sender, PaintEventArgs e)
    {
        drawing = new GridDrawing(this, bmpPic, 6, 8); // Form, Bitmap, Rows, Columns
        foreach (var piece in drawing.Pieces)
        {
            e.Graphics.DrawImage(bmpPic, piece.Position);
        }
    }

    private void btnStart_Click(object sender, PaintEventArgs e)
    {
    }
}

GridDrawing.cs GridDrawing.cs

    public class GridDrawing
{
    private Bitmap bmpPic;
    private int columns;
    private int rows;
    private List<GridPiece> pieces;

    private Point position;

    /// <summary>
    /// Constructs a grid with dots.
    /// </summary>
    /// <param name="ctrl"></param>
    /// <param name="gridPic"></param>
    /// <param name="rows"></param>
    /// <param name="columns"></param>
    public GridDrawing(Control ctrl, Bitmap bmpPic, int rows, int columns)
    {
        this.bmpPic = bmpPic;                                         //The picture(Dot).
        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
        Point position = new Point(0, 0);                                               //Draw position of the picture(Dot)
        Size size = new Size(bmpPic.Width, bmpPic.Height);                              //Size of picture(Dot).

        for (int i = 0; i <= rows; i++)             //A  with 6 rows
        {
            position.X = 0;                         //Puts the value X on 0 when it starts a new row.
            for (int j = 0; j <= columns; j++)      //A matrix with 8 columns
            {
                GridPiece s = new GridPiece(bmpPic, position);  // Creates a piece
                pieces.Add(s);                                  // Puts the piece that has to be drawn in the List<GridPiece>pieces
                position.X += size.Width;                       // Changes the width of the draw position
            }
            position.Y += size.Height;                          // Changes the height of the draw position
        }
    }

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

GridPiece.cs GridPiece.cs

public class GridPiece
{
    private Bitmap bmpPic;
    private Point position;

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

    public Point Position
    {
        get { return position; }
    }
}

Could anyone pretty please help me solve my issue? 漂亮的人能帮我解决我的问题吗? I updates the code several times. 我多次更新代码。

You need to handle the form's Paint event, and use the methods in e.Graphics to draw your pieces in a loop. 您需要处理表单的Paint事件,并使用e.Graphics的方法来循环绘制您的作品。

(Probably e.Graphics.DrawImage , or perhaps FillCircle ) (可能是e.Graphics.DrawImageFillCircle

It would look something like 看起来像

void Form_Paint(object sender, PaintEventArgs e) { 
    foreach(var piece in drawing.Pieces) {
        e.Graphics.DrawImage(bmpPic, piece.Position);
    }
}

The paint event is raised every time the forms needs to be drawn. 每次需要绘制表格时都会引发绘画事件。

When the pieces move, you need to manually force the form to repaint by calling Invalidate() . 当碎片移动时,您需要通过调用Invalidate()来手动强制重新绘制表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM