如何在Panel上绘制以使其不闪烁?

这是我的代码。 当我将光标移动到Form上时,圆圈正在移动,但它正在闪烁。 我怎样才能解决这个问题?

public partial class Preprocesor : Form { int x, y; Graphics g; public Preprocesor() { InitializeComponent(); } private void Preprocesor_Load(object sender, EventArgs e) { g = pnlMesh.CreateGraphics(); } private void pnlMesh_Paint(object sender, PaintEventArgs e) { g.Clear(Color.White); g.FillEllipse(Brushes.Black, x, y, 10, 10); } private void pnlMesh_MouseMove(object sender, MouseEventArgs e) { x = eX; y = eY; pnlMesh.Invalidate(); } } 

您需要使用双缓冲控件。

创建一个inheritanceControl并设置DoubleBuffered = true; 在构造函数中(这是受保护的属性)。
使用该控件而不是面板,它不会有任何闪烁。

此外,您不应该存储Graphics对象以供日后使用。
相反,你应该在Paint处理程序中绘制e.Graphics

bout如何覆盖面板用户控件并将Doublebuffered设置为true?

 public partial class BufferPanel : Panel { public BufferPanel() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); UpdateStyles(); } }