[英]Winform graphic rectangle large numbers and show them on form/panel/tab
我需要在 winform 上绘制大小为 (10, 10 ) 的两种不同颜色(红色和蓝色)的动态矩形。 它的数量可以超过 100,000+。
当我在 winform 或面板中绘制它们时,它们开始重叠。 我试过滚动条,但我无法做到这一点。 当我垂直滚动它们时,形状开始变得混乱。 矩形的数量可能很多(行和列)。
private void button1_Click(object sender, EventArgs e)
{
tabControl2.Visible = true;
Graphics g = FileInfoTab.CreateGraphics();
Pen p = new Pen(Color.Black);
for (int y = 158; y < 1000; y += 15)
{
for (int x = 120; x < 280; x += 15)
{
Random rd = new Random();
int nm = rd.Next(0, 10);
if (nm % 2 == 0) //if number is even draw red rectangle else blue rectangle
{
SolidBrush sb = new SolidBrush(Color.Red);
g.DrawRectangle(p, x, y, 10, 10);
g.FillRectangle(sb, x, y, 10, 10);
//x += 15;
}
else
{
SolidBrush sb_1 = new SolidBrush(Color.Blue);
g.DrawRectangle(p, x, y, 10, 10);
g.FillRectangle(sb_1, x, y, 10, 10);
}
}
}
}
从Control.CreateGraphics()
创建的Graphics
对象必须始终在您的代码退出之前处理,如下所述:( CreateGraphics() 方法和 Paint Event Args )
也许您应该考虑使用 FileInfoTab.Paint 事件:
private Bitmap Surface;
private Graphics g;
private void button1_Click(object sender, EventArgs e)
{
RedrawSurface();
}
private void RedrawSurface()
{
tabControl2.Visible = true;
Surface = new Bitmap(FileInfoTab.Width, FileInfoTab.Height, PixelFormat.Format24bpprgb);
g = Graphics.FromImage(Surface);
Pen p = new Pen(Color.Black);
for (int y = 158; y < 1000; y += 15)
{
for (int x = 120; x < 280; x += 15)
{
Random rd = new Random();
int nm = rd.Next(0, 10);
if (nm % 2 == 0)
{
SolidBrush sb = new SolidBrush(Color.Red);
g.DrawRectangle(p, x, y, 10, 10);
g.FillRectangle(sb, x, y, 10, 10);
//x += 15;
}
else
{
SolidBrush sb_1 = new SolidBrush(Color.Blue);
g.DrawRectangle(p, x, y, 10, 10);
g.FillRectangle(sb_1, x, y, 10, 10);
}
}
}
g.Dispose();
FileInfoTab.Invalidate();
}
private void FileInfoTab_Paint(object sender, PaintEventArgs e)
{
if (Surface != null)
{
e.Graphics.DrawImage(Surface, 0, 0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.