显示不起作用
Shown doesn't work
public void GridCreate()
{
Graphics g = pictureBox1.CreateGraphics();
SolidBrush brushBlack = new SolidBrush(Color.Black);
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i <= block.GetLength(0) - 1; i++)
{
for (int n = 0; n <= block.GetLength(0) - 1; n++)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
g.FillRectangle(brushBlack, block[n, i]);
}
}
data.block = block;
}
private void Form1_Shown(object sender, EventArgs e)
{
GridCreate();
}
我正在尝试使用 PictureBox 在 WindowsForms 中制作网格,但相关代码无法正常工作 correctly.This data.block = block;
部分有效,但此 g.FillRectangle(brushBlack, block[n, i]);
根本无效.我认为问题出在 Form1_Shown
事件中,因为:
private void Form1_Click(object sender, EventArgs e)
{
GridCreate();
}
执行得很好。
覆盖 protected override void OnShown(EventArgs e)
给出与 Form1_Shown
相同的结果。
问题是 CreateGraphics()
,这是一个 临时 表面,当 PictureBox 刷新自身时会被擦除。
只需创建一次网格,然后在Paint()事件中绘制数据:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GridCreate();
pictureBox1.Paint += pictureBox1_Paint;
}
private void GridCreate()
{
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i < block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
}
}
data.block = block;
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics; // use the SUPPLIED graphics, NOT CreateGraphis()!
for (int i = 0; i < data.block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < data.block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
g.FillRectangle(Brushes.Black, data.block[n, i]);
}
}
}
public void GridCreate()
{
Graphics g = pictureBox1.CreateGraphics();
SolidBrush brushBlack = new SolidBrush(Color.Black);
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i <= block.GetLength(0) - 1; i++)
{
for (int n = 0; n <= block.GetLength(0) - 1; n++)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
g.FillRectangle(brushBlack, block[n, i]);
}
}
data.block = block;
}
private void Form1_Shown(object sender, EventArgs e)
{
GridCreate();
}
我正在尝试使用 PictureBox 在 WindowsForms 中制作网格,但相关代码无法正常工作 correctly.This data.block = block;
部分有效,但此 g.FillRectangle(brushBlack, block[n, i]);
根本无效.我认为问题出在 Form1_Shown
事件中,因为:
private void Form1_Click(object sender, EventArgs e)
{
GridCreate();
}
执行得很好。
覆盖 protected override void OnShown(EventArgs e)
给出与 Form1_Shown
相同的结果。
问题是 CreateGraphics()
,这是一个 临时 表面,当 PictureBox 刷新自身时会被擦除。
只需创建一次网格,然后在Paint()事件中绘制数据:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GridCreate();
pictureBox1.Paint += pictureBox1_Paint;
}
private void GridCreate()
{
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i < block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
}
}
data.block = block;
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics; // use the SUPPLIED graphics, NOT CreateGraphis()!
for (int i = 0; i < data.block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < data.block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
g.FillRectangle(Brushes.Black, data.block[n, i]);
}
}
}