Panel的DoubleBuffered属性停止了绘图,是不可见的

发布于 2024-10-19 21:08:54 字数 1035 浏览 4 评论 0原文

我有一个组件,一个继承的面板,我在其中重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2024-10-26 21:08:54

尝试使用 ControlStyles.OptimizedDoubleBuffered 代替。它速度更快,通常效果更好。确保 ControlStyles.AllPaintingInWmPaintControlStyles.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();
    }
}

Try using ControlStyles.OptimizedDoubleBuffered instead. It's faster and usually works better. Make sure ControlStyles.AllPaintingInWmPaint and ControlStyles.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 using Refresh(); you must never call OnPaint() yourself. Do not dispose the Graphics object. If you fail any of those conditions, flickering and various other drawing bugs might occur.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文