3

While using Panel control in TabPage of Tab control, I have quite a few child controls like RichTextBox, Buttons, Labels etc.

Problem is when I scroll in the Panel, there is a flickering inside. The child controls are not being shown/drown/painted smoothly like they are already there.

Looking for something that could make this scrolling smooth and remove the flickering effect. Any suggestions would help a lot. I tried a several other methods like DoubleBuffered, but didn't really work.

Indigo
  • 2,887
  • 11
  • 52
  • 83

1 Answers1

3

Well I solved the problem with combination of different other suggestions, below is the code that removed flickering for me, essentially making it DoubleBuffered using Win API.

References here and here.

public partial class SmoothScrollPanel : UserControl
{
    public SmoothScrollPanel()
    {
        InitializeComponent();
        // this.DoubleBuffered = true;
    }

    private const int WM_HSCROLL = 0x114;
    private const int WM_VSCROLL = 0x115;

    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
        && (((int)m.WParam & 0xFFFF) == 5))
        {
            // Change SB_THUMBTRACK to SB_THUMBPOSITION
            m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF) | 4);
        }
        base.WndProc(ref m);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;    // Turn on WS_EX_COMPOSITED
            return cp;
        }
    }
}
Community
  • 1
  • 1
Indigo
  • 2,887
  • 11
  • 52
  • 83
  • I'm curious: Do you know what the fiddlin with the Scrolling does and do you see a difference compared to simply setting DoubleBuffered to true in a sub-classed Panel? – TaW Jul 02 '14 at 11:36
  • @TaW: Hummm, I really don't know what makes it different when settings this.DoubleBuffered = true; and through Win API. Seems to have worked for me. I can't see the flickering anymore. But if I simply use this.DoubleBuffered = true; that wouldn't work, I get flickering. BTW, my bad, I didn't use this.DoubleBuffered = true; in my solution. Updating answer now. It works without that just using Win API, flickering is gone. – Indigo Jul 02 '14 at 11:43