private bool[,] struArr = new bool[5, 5];
private Color blockColor = Color.Red;
private void lblMode_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
gp.Clear(Color.Black);
Pen p = new Pen(Color.White);
for (int i = 31; i < 155; i = i + 31)//竖线
{
gp.DrawLine(p, i, 1, i, 155);
}
for (int i = 31; i < 155; i = i + 31) //横线
{
gp.DrawLine(p, 1, i, 155, i);
}
SolidBrush s = new SolidBrush(blockColor);
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
if (struArr[x, y])
{
gp.FillRectangle(s, 31 * x + 1, 31 * y + 1, 30, 30);
}
}
}
}
private void lblMode_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
else
{
int xPos, yPos;
xPos = e.X / 31;
yPos = e.Y / 31;
struArr[xPos, yPos] = !struArr[xPos, yPos];
bool b = struArr[xPos, yPos];
Graphics gp = lblMode.CreateGraphics();
SolidBrush s = new SolidBrush(b ? blockColor : Color.Black);
gp.FillRectangle(s, 31 * xPos + 1, 31 * yPos + 1, 30, 30);
gp.Dispose();
}
}