I have a panel that I've subclassed to and have set DoubleBuffered
true, I constantly need to refresh the drawing but it flickers and have no idea why.
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();
}
So after every "computation" and have added something to my imaginary grid, I need to update the panel to show this new item on my grid. I have tried invalidating the panel right before the displayGrid()
function but it seems to cause even more flickering.
The heartbeat()
function is currently being called on a separate thread.
Here is my new Panel
class.
public class Display : Panel
{
public Display()
{
this.DoubleBuffered = true;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…