How do I double buffer a Panel?

前端 未结 5 1819
一个人的身影
一个人的身影 2020-11-29 06:38

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

相关标签:
5条回答
  • 2020-11-29 07:06

    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
    
    0 讨论(0)
  • 2020-11-29 07:10

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:18

    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:30

    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

    0 讨论(0)
  • 2020-11-29 07:31

    Winform panels have a DoubleBuffered property.

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

    0 讨论(0)
提交回复
热议问题