在表格上绘制作品列表

时间:2011-04-07 15:41:20

标签: c#

首先,我是C#世界的新手。 今年我刚刚开始学习C#,没有编程背景经验。 我带着一个问题来到这里,我想尽快解决。 我设法写了一些代码来创建一个包含绘图位置的Bitmaps列表。 我想要做的是在表格的列表中绘制每一块(点的图片)。 我一直在努力工作,试图找出如何在表格上绘制列表...... 我在代码背后放置了评论,以便读者更容易理解它是什么,因此它不会对读者造成任何脑损伤或汗水。 :p 请参阅下面的代码:

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

    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

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; }
    }
}

有人可以帮助我解决我的问题吗? 我多次更新代码。

1 个答案:

答案 0 :(得分:0)

您需要处理表单的Paint事件,并使用e.Graphics中的方法在循环中绘制您的作品。

(可能e.Graphics.DrawImage,或者FillCircle

看起来像

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

每次需要绘制表单时都会引发paint事件。

当碎片移动时,您需要通过调用Invalidate()手动强制重新绘制表单。