4

I have a form in which I load a user control that contains 3 other user controls in it.

Every time I move to another tab and return to that user control the 3 controls inside it flickers even befor any event fires.

I tried every thing including:

this.DoubleBuffered = true;
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

and

protected override CreateParams CreateParams
{
    get
    {
        CreateParams baseParams = base.CreateParams;
        baseParams.ExStyle |= 0x0200000;
        return baseParams;
    }
}

and

protected override CreateParams CreateParams
{
    get
    {
        CreateParams baseParams = base.CreateParams;
        baseParams.ExStyle &= 0x0200000;
        return baseParams;
    }
}

But nothing helps. The flickering happens befor any code runs.

What can be the problem?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Liran Friedman
  • 4,027
  • 13
  • 53
  • 96
  • 1
    Does this only happen when you run the project in debug mode? I have had similar issues in the past, but they weren't an issue when ran in release mode and/or outside of Visual Studio. Another less likely issue could be your display adapter drivers, I have seen that being the problem before, but admittedly only once. – XN16 Jan 20 '14 at 10:22
  • 1
    The given info is quite inadequate to diagnose a flicker issue. Hacking WS_EX_COMPOSITED cannot work when you do that in a user control, it must be a style flag that's turned on in a toplevel window. The form. – Hans Passant Jan 20 '14 at 10:26
  • I am compailing in debug, but it happens even whn I am running only the exe file outside Visual Studio. – Liran Friedman Jan 20 '14 at 10:29
  • I tried placing the hack WS_EX_COMPOSITED every where in the code. In the main form, in the control... every where. And still nothing... – Liran Friedman Jan 20 '14 at 10:30
  • @LiranFridman You say you compile in debug, but have you tried it in release then ran outside Visual Studio? Just to make sure you have tried as that has been a solution to several of my more annoying issues in the past. – XN16 Jan 20 '14 at 10:33
  • I tried now running in release and even running out of Visual Studio, and still the same pronblem... – Liran Friedman Jan 20 '14 at 11:39

2 Answers2

4

This might be happening because the background is being painted every time.

If all your painting is done in OnPaint() (including clearing the background - which your nested user controls are probably doing themselves, so that should be ok), then you can completely disable the background painting as follows:

protected override void OnPaintBackground(PaintEventArgs pevent)
{
    // Do nothing to disable background painting.
}

You might find that you need to also do that in the implementation of one or more of your nested user controls.

Also, if you are customising an existing Windows control to prevent it from flickering, you sometimes have to do the following (but this is NOT something you should do for one of your user controls):

private const int WM_ERASEBKGND = 20;

[PermissionSet(SecurityAction.Demand, Unrestricted=true)]

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_ERASEBKGND)
    {
        m.Result = IntPtr.Zero;
    }
    else
    {
        base.WndProc(ref m);
    }
}

I had to do that when customising a ListView to prevent it from flickering.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

If you're drawing lots of elements in the tabs, you might try disabling and resuming the "redraw" as described here.

Community
  • 1
  • 1
henginy
  • 2,041
  • 1
  • 16
  • 27