views:

22

answers:

2

I have noticed that if I have a TabControl in a Panel that has an Image Background, when the mouse hovers over a tab it blinks and redraws. Is there a workaround to prevent this from happening?

+1  A: 

I see it. It happens because TabControl draws itself partly by asking the parent control to draw itself inside its own window. Necessary because the tabs don't cover the full width of the control, they "stick out". If the BackgroundImage is slow to draw, you'll see a flicker between the background being drawn and the tabs drawn on top of that.

This is going to be hard to fix, TabControl doesn't support any kind of double buffering. You can only minimize the effect by making the BackgroundImage efficient to draw. You need to do so by making the image exactly the size of the panel's ClientSize so that the image doesn't have to be resized. And create that bitmap with the PixelFormat32bppPArgb pixel format, it is usually 10 times faster than the other ones.

There's one magic cure available, windows have a style flag that enables double-buffering for the entire window, including its child controls. Supported since XP but some side-effects have been reported. Paste this code into your form, it fixes the TabControl flicker:

    protected override CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
            return cp;
        }
    }
Hans Passant
That did the trick, thanks!
jwarzech
Forgot to mark the response as the answer, this solved my problem thanks :0)
jwarzech
A: 

You can try creating a panel that uses double buffering. Derive from panel and set DoubleBuffered to true:

   public partial class DoubleBufferedPanel : Panel
   {
      public DoubleBufferedPanel()
      {
         InitializeComponent();

         this.DoubleBuffered = true;

         UpdateStyles();
      }
   }
msergeant