Preventing screen flicker when using opacity

The first time you change the opacity on a form (through code) the screen flickers, but any subsequent opacity changes while the form is still alive are smooth. Why is this

Im trying to write some simple code to fade my app out when it closes:

                For i = 1.0 To 0.01 Step -0.05
                    Me.Opacity = i
                    Application.DoEvents()
                Next
                End

Works beautifully but it looks bad because the screen flickers before it starts fading... ive tried using Lockwindow api to try and stop it but it didnt work, it works smoothly if i put this code in form_load:

        Me.Opacity = 0.9
        Me.Opacity = 1

But the problem there is i get flicker when the form loads up... is there any way around this  Im a perfectionist... this is unacceptable! :P


Answer this question

Preventing screen flicker when using opacity

  • Bezpol

    Nismo,

    I'm having the same problem. Did you ever find a solution

    I'm using Visual Studio 2005 Beta 1. When an exception is unhandled and you are thrown into the debugger, the window that shows the problem (the one with the leader drawn pointing to the line of offending code) becomes transparent while you drag it around. I noticed that this window also has the flickering problem.

    However, when you move controls around using the Windows Forms designer, these are also transparent while being dragged, but there is no flicker, which leads me to believe that there is a solution to be found, if only we can find it!

    Regards,

    Yellow Banana.

  • Turnbuckle

    If a window is created it is in a so called "normal mode". If you set Opacity < 1 the window will be changed to so called "layered mode". This will affect this flickering. You can prevent this, by set Opacity to 0.9 and back to 1 bevor you show the window the first time.

    Frank

  • Rob G

    First of all: Don't do that!!! I'm referring to a For loop with a DoEvents call inside. That's pretty much how you would do something in a non-preemptive multitasking Windows 3.1-style world!

    Use a Timer instead! At least if you are a perfectionist who wants your code to be up-to-date with the 1990's... sorry... 2000's...


    Second: Try setting the opacity to something like 99% in design-time to make it layered from the beginning. Then in the Load event you set it right up to 100%.


  • Preventing screen flicker when using opacity