0
首先,我是C#世界的新手。 我今年剛剛開始學習C#,沒有編程背景經驗。 我來到這裏是想盡快解決問題。 我設法寫一點代碼來創建一個帶位圖的列表,包括繪製位置。 我想要做的是在表單上的列表中繪製每一塊(點的圖片)。 我一直在這個工作時間只是想弄清楚如何獲得在窗體上繪製的列表... 我把意見放在代碼後面,使讀者更容易理解它是什麼,所以它不會給讀者造成任何腦損傷或出汗。 :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; }
}
}
任何人都可以請幫我解決我的問題? 我更新了幾次代碼。
請給我一個示例代碼,以便我更容易理解? :) – Konni 2011-04-07 15:47:52
我的態度在哪裏,首先我要感謝你的時間。 我試過你的例子,我編輯原始帖子,所以你可以看到我做了什麼。 piece.position指出它不可訪問的二重奏保護級別。我已經試圖公開(暫時只是爲了測試)。任何提示來解決這個問題? – Konni 2011-04-07 16:09:45
你需要讓它們公開。你應該公開只讀屬性,揭露它們 – SLaks 2011-04-07 16:11:01