我有一个面板,我已经子类化并设置为DoubleBuffered
true,我经常需要刷新绘图,但它闪烁并且不知道为什么。
private delegate void MyDelegate();
public void heartBeat()
{
while (true)
{
if (map.processNubots(rules))
{
if (this.InvokeRequired)
{
this.Invoke((MyDelegate)delegate
{
//drawPanel.SuspendLayout();
drawPanel.Refresh();
displayGrid();
//drawPanel.ResumeLayout();
});
}
Thread.Sleep(500);
}
else
{
break;
}
}
}
public void displayGrid()
{
int i = 0;
foreach (DictionaryEntry pair in map)
{
Monomer current = (Monomer)pair.Value;
drawMonomers(current.getLocation(), current.getState());
i++;
}
}
public void drawMonomers(Point location, string state)
{
...
SolidBrush sb = new SolidBrush(mycolor);
SolidBrush sbt = new SolidBrush(Color.Black);
Graphics g = drawPanel.CreateGraphics();
Font text = new Font("Arial", scale / 2);
Pen pen = new Pen(Color.Black, 1);
pen.Alignment = PenAlignment.Inset;
g.FillEllipse(sb, offSet + ((location.Y * scale) / 2) + (location.X * scale), offSet + (-location.Y * scale), scale, scale);
g.DrawEllipse(pen, offSet + ((location.Y * scale) / 2) + (location.X * scale), offSet + (-location.Y * scale), scale, scale);
g.DrawString(state, text, sbt, (offSet + ((location.Y * scale) / 2) + (location.X * scale)) + scale / 6, (offSet + (-location.Y * scale)) + scale / 6);
sb.Dispose();
sbt.Dispose();
pen.Dispose();
}
因此,在每次“计算”并在我的想象网格中添加了一些内容之后,我需要更新面板以在我的网格上显示这个新项目。我曾尝试在该功能之前使面板无效,displayGrid()
但它似乎会导致更多闪烁。
该heartbeat()
函数当前正在一个单独的线程上调用。
这是我的Panel
新课。
public class Display : Panel
{
public Display()
{
this.DoubleBuffered = true;
}
}