一.创建工程项目
1.创建新项目
2.点击左侧隐藏工具栏添加picture box 和 button 两个工具到窗体页面如下图所示:
1.双击button为按钮添加点击响应;添加代码如下:
namespace WindowsForms1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
2.围棋棋盘为324个小正方形所围成的大正方形,格数为18*18,每个小正方形长宽为1厘米;编写第一行18个小正方形代码如下:
private void button1_Click(object sender, EventArgs e)
{
Point pt0 = new Point(100, 100);
int width = 20;
var g = pictureBox1.CreateGraphics();
g.Clear(Color.White); //定义背景为白色
var pen = new Pen(Color.Black);//定义画笔为黑色
g.DrawRectangle(pen,pt0.X, pt0.Y, width * 18, width * 18);
利用for循环实现下面17行的操作:
for (int line=0;line<18;line++) //定义18次循环
{
int i = 0;
for (i=0; i < 18; i++)
{
g.DrawRectangle(pen, i * 1 * width + pt0.X, 1 * line * width + pt0.Y, width, width);
}
3. 实现围棋棋盘
namespace WinForms1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Point pt0 = new Point(100, 100);
int width = 20;
var g = pictureBox1.CreateGraphics();
g.Clear(Color.White);
var pen = new Pen(Color.Black);
g.DrawRectangle(pen,pt0.X, pt0.Y, width * 18, width * 18);
for (int line=0;line<18;line++)
{
int i = 0;
for (i=0; i < 18; i++)
{
g.DrawRectangle(pen, i * 1 * width + pt0.X, 1 * line * width + pt0.Y, width, width);
}
for(i=0;i<18;i++)
{
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}