我有一个组件,一个继承的面板,在那里我重写了OnPaint事件来绘制一个有500个点的图。因为我需要在图表上做一些选择,所以它是闪烁的。我已经找到了这个DoubleBuffered属性,但是当我在面板构造函数中将它设置为True时,绘图就消失了。我对它进行了调试,我看到绘图方法仍然在执行,但是面板上什么也没有。有人知道为什么会发生这种事吗?
这是.NET 3.5 - C#。Winforms应用程序
try
{
Graphics g = e.Graphics;
//Draw _graphArea:
g.DrawRectangle(Pens.Black, _graphArea);
_drawingObjectList.DrawGraph(g, _mainLinePen, _diffLinePen, _dotPen, _dotBrush, _notSelectedBrush, _selectedBrush);
DrawSelectionRectangle(g);
g.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}Panel派生构造函数:
this.BackColor = Color.White;
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.UpdateStyles(); 发布于 2011-03-02 00:26:13
请尝试使用ControlStyles.OptimizedDoubleBuffered。它速度更快,而且通常效果更好。确保还启用了ControlStyles.AllPaintingInWmPaint和ControlStyles.UserPaint。
现在,OnPaint()应该是绘制到窗口中的惟一东西,并且该方法只能从无效调用或使用Refresh()调用;您永远不能自己调用OnPaint()。不要释放Graphics对象。如果不符合这些条件中的任何一个,可能会出现闪烁和各种其他绘图错误。
class MyControl : UserControl
{
public MyControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.Red);
}
void RandomEventThatRequiresRefresh(object sender, EventArgs e)
{
Refresh();
}
}https://stackoverflow.com/questions/5155111
复制相似问题