如何对面板进行双缓冲?

发布于 2024-07-18 22:40:12 字数 161 浏览 4 评论 0原文

我有一个带有轮盘赌轮的面板,我需要对面板进行双缓冲,以使其停止闪烁。 谁能帮我吗?

编辑:

是的,我已经尝试过了。

panel1.doublebuffered不存在,只有this.doublebuffered。 我不需要缓冲表单,只需缓冲面板。

I have a panel that has a roulette wheel on it, and I need to double buffer the panel, so that it stops flickering. Can anyone help me out?

EDIT:

Yes, I have tried that.

panel1.doublebuffered does not exist, only this.doublebuffered. And I don't need to buffer the Form, just the Panel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

情何以堪。 2024-07-25 22:40:12

另一种方法是使用 InvokeMember 方法调用成员 doublebuffered:

 typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty    
            | BindingFlags.Instance | BindingFlags.NonPublic, null,
            panel2, new object[] { true }); 

通过这种方式,您不必创建子类

Another way of doing this is to invoke the member doublebuffered, using the InvokeMember method:

 typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty    
            | BindingFlags.Instance | BindingFlags.NonPublic, null,
            panel2, new object[] { true }); 

By doing it this way, you don't have to create a subclass

坐在坟头思考人生 2024-07-25 22:40:12

您需要从Panel 或PictureBox 派生。

这会产生一些影响,具体取决于您选择启用缓冲的方式。

如果你设置了 this.DoubleBuffer 标志那么你应该没问题。

如果您手动更新样式,那么您必须自己在 WM_PAINT 中绘制表单。

如果您确实雄心勃勃,您可以维护并绘制自己的后台缓冲区作为位图。


using System.Windows.Forms;

public class MyDisplay : Panel
{
    public MyDisplay()
    {
        this.DoubleBuffered = true;

        // or

        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        UpdateStyles();
    }
}

You need to derive from Panel or PictureBox.

There are ramifications to this depending on how you choose to enable the buffering.

If you set the this.DoubleBuffer flag then you should be ok.

If you manually update the styles then you have to paint the form yourself in WM_PAINT.

If you really feel ambitious you can maintain and draw your own back buffer as a Bitmap.


using System.Windows.Forms;

public class MyDisplay : Panel
{
    public MyDisplay()
    {
        this.DoubleBuffered = true;

        // or

        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        UpdateStyles();
    }
}
只是在用心讲痛 2024-07-25 22:40:12

您可以在 Panel 的派生类中将 DoubleBuffered-Property 设为公共:

public class DoubleBufferedPanel : Panel
{        
    [DefaultValue(true)]
    public new bool DoubleBuffered
    {
        get
        {
            return base.DoubleBuffered;
        }
        set
        {
            base.DoubleBuffered = value;
        }
    }
}

You can make the DoubleBuffered-Property public in a derivided class of Panel:

public class DoubleBufferedPanel : Panel
{        
    [DefaultValue(true)]
    public new bool DoubleBuffered
    {
        get
        {
            return base.DoubleBuffered;
        }
        set
        {
            base.DoubleBuffered = value;
        }
    }
}
北城孤痞 2024-07-25 22:40:12

Winform 面板具有 DoubleBuffered 属性

编辑:我应该注意到它受到保护。 其他人已经描述了如何对其进行子类化。 :)

Winform panels have a DoubleBuffered property.

Edit: I should have noticed that it was protected. Others have described how to sub-class it. :)

秋心╮凉 2024-07-25 22:40:12

只是扩展 User79775 的答案,如果你想在 VB.net 中实现这一点,请这样做:

Imports System.Windows.Forms

Public Class MyDisplay
    Inherits Panel

    Public Sub New()
        Me.DoubleBuffered = True

        ' or

        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        UpdateStyles()
    End Sub
End Class

Just expanding on User79775's answer, if you're trying to achieve this in VB.net, do so like this:

Imports System.Windows.Forms

Public Class MyDisplay
    Inherits Panel

    Public Sub New()
        Me.DoubleBuffered = True

        ' or

        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        UpdateStyles()
    End Sub
End Class
面如桃花 2024-07-25 22:40:12

我和你遇到了完全相同的困境。 我将一个面板放入 C# WinForm 中,并将其用作几何动画的显示区域。 由计时器触发的多次重绘操作,加上偶尔调整窗口大小,都会导致面板严重闪烁。 这里提供的其他解决方案似乎都依赖于激活 DoubleBuffered 标志,但这对我的经验没有任何影响。

正确的方法是使用 System.Drawing.BufferedGraphics 类并在应用程序中利用它。

这是概述< /a> ,以及一个 示例实际上有效(不再闪烁!)。

这个例子有点矫枉过正,因为它强调了在翻转图形缓冲区之前至少有两种执行渲染的方法; 您只需要在实施中保留您喜欢的那个即可。

我个人选择将 Panel 子类化为新的 DoubleBufferedPanel 类。 我只是使用基本面板的 Paint() 方法并结合调用 Refresh() 来翻转缓冲区。

I was in the exact same predicament as you. I put a Panel inside of a C# WinForm and used it as a display zone for a geometric animation. Multiple redraw operations triggered by a timer, combined with the occasional window resizing, all caused that Panel to flicker terribly. The other solutions offered here all seem to rely on activating the DoubleBuffered flag, but this never made any difference in my experience.

The proper way to go about it is to use the System.Drawing.BufferedGraphics class and harness it in your application.

Here's an overview of it, and an example that actually works (no more flickering!).

That example is a bit overkill since it highlights at least two ways to perform the rendering before flipping the graphic buffers; you only need to keep the one you prefer in your implementation.

I personally chose to subclass the Panel into a new DoubleBufferedPanel class. I simply use the base Panel's Paint() method combined with a call to Refresh() to flip the buffers.

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