新手们的GDI+绘制方格
//绘制panel控件触发的事件
//不可在窗体加载时绘制方格
private void panel1_Paint(object sender, PaintEventArgs e)
{
int rowNum = 12;//行数
int colNum = 15;//列数;
Pen pen = new Pen(Color.Black);//实例化一个“画笔”
Brush brushes = new SolidBrush(Color.SkyBlue);//“画刷”
Graphics g = Graphics.FromHwnd(this.panel1.Handle);//创建一个句柄
int width = this.panel1.Width / colNum;//panel控件的宽度/列数==宽度
int height = this.panel1.Height / rowNum;//panel控件的高度/行数==高度
int sideLength = width > height ? height : width;//格子的边长
for (int rowIndex = 0; rowIndex < rowNum; rowIndex++)//循环行数
{
for (int colIndex = 0; colIndex < colNum; colIndex++)//循环列数
{
g.DrawRectangle(pen, new Rectangle(colIndex * sideLength, rowIndex * sideLength, sideLength, sideLength));
g.FillRectangle(brushes, new Rectangle(colIndex * sideLength + 1, rowIndex * sideLength + 1, sideLength - 1, sideLength - 1));
}
}
}
搞定收工,这些适合新手们的参考。希望可以帮到您!