Panel的DoubleBuffered属性停止了绘图,是不可见的
我有一个组件,一个继承的面板,我在其中重写 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);
}
面板后代构造函数:
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();
I have a component, an inherited Panel, where I am overriding the OnPaint event to draw a graph with 500 points. Since I need to do some selection on the graph, it is flickering. I have found this DoubleBuffered property but when I set it to True, in the panel constructor, the drawing disappears. I debug it and I see that the drawing methods still execute but there is nothing on the panel.
Does anyone know why would this happen?
This is .NET 3.5 - C#. Winforms application
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 descendant constructor:
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
ControlStyles.OptimizedDoubleBuffered
代替。它速度更快,通常效果更好。确保ControlStyles.AllPaintingInWmPaint
和ControlStyles.UserPaint
也已启用。现在,
OnPaint()
应该是唯一绘制到窗口的内容,并且只能在失效时或使用Refresh()
调用此方法;您绝不能自己调用OnPaint()
。不要处置Graphics
对象。如果您未满足任何这些条件,则可能会出现闪烁和各种其他绘图错误。Try using
ControlStyles.OptimizedDoubleBuffered
instead. It's faster and usually works better. Make sureControlStyles.AllPaintingInWmPaint
andControlStyles.UserPaint
are also enabled.Now,
OnPaint()
should be the only stuff that draw to the window and this method must only be called from invalidation or by usingRefresh()
; you must never callOnPaint()
yourself. Do not dispose theGraphics
object. If you fail any of those conditions, flickering and various other drawing bugs might occur.