如何在C#中绘制25个矩形(5*5)?
我之后需要能够访问特定的矩形并更改它的颜色,例如如果用户输入不正确的单词,则更改颜色为红色。
在这种情况下,创建一个矩形数组是否更合适?
这是我目前的代码:
Graphics g = pictureBox1.CreateGraphics();
int x =0;
int y= 0;
int width = 20;
int height = 20;
for (int i = 0; i < 25; i++)
{
if (i <= 4)
{
g.FillRectangle(Brushes.Blue, x, y, width, height);
x += 50;
}
else if (i > 4)
{
y = 50;
g.FillRectangle(Brushes.Blue, x, y, width, height);
x += 50;
}
}
for (int yPos = 0; yPos < 5; yPos++) { for (int xPos = 0; xPos < 5; xPos++) { /* 在这里编写代码 */ }
- Kieren Johnstonecontrol.CreateGraphics
!永远不要尝试缓存绑定到控件的Graphics
对象!可以使用Graphics g = Graphics.FromImage(bmp)
在Bitmap bmp
中进行绘制,或者在控件的Paint
事件中使用e.Graphics
参数进行绘制。您可以通过执行最小化/最大化序列来测试图形的持久性。 - TaW