Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Fading a Form

Fading a Form

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpquestionlearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucidation
    wrote on last edited by
    #1

    I'm new to C++ and still learning a ton, and I'm hoping that someone can point me in the right direction to solve a problem (my Google-fu has failed me). I have a Windows form that displays on the the screen, and everything works just fine. My problem is that I want it to fade in when it's opened, and fade out when it's closed. I can't seem to find any good C++ examples (that I can understand). Can anyone shed some light on this for me? Thanks!

    P 1 Reply Last reply
    0
    • L Lucidation

      I'm new to C++ and still learning a ton, and I'm hoping that someone can point me in the right direction to solve a problem (my Google-fu has failed me). I have a Windows form that displays on the the screen, and everything works just fine. My problem is that I want it to fade in when it's opened, and fade out when it's closed. I can't seem to find any good C++ examples (that I can understand). Can anyone shed some light on this for me? Thanks!

      P Offline
      P Offline
      Philippe Mori
      wrote on last edited by
      #2

      Uses a timer and increase the opacity until the form is fully opaque.

      Philippe Mori

      L 1 Reply Last reply
      0
      • P Philippe Mori

        Uses a timer and increase the opacity until the form is fully opaque.

        Philippe Mori

        L Offline
        L Offline
        Lucidation
        wrote on last edited by
        #3

        I don't understand your reply. I already tried something like this:

        private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e){
        for(double i = 0; i < 1.05; i += 0.05){
        this->Opacity = i;
        this->Refresh();
        }
        }

        But that didn't do anything. Can someone give me a little more info? Thanks.

        L 1 Reply Last reply
        0
        • L Lucidation

          I don't understand your reply. I already tried something like this:

          private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e){
          for(double i = 0; i < 1.05; i += 0.05){
          this->Opacity = i;
          this->Refresh();
          }
          }

          But that didn't do anything. Can someone give me a little more info? Thanks.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          There are at least two problems with your code: 1. there is no delay in your loop, it will sprint from 0 to 1 as fast as it can, all will be over in a fraction of a second. 2. the Form isn't visible while the Load handler is executing; it is only when Load is done that the Form becomes visible, so this is the wrong place for such loop. The recommended solution to both these issues is as follows: - give your Form an initial opacity of zero; - in your Load handler, start a Windows.Forms.Timer with an interval of say 100 msec; - in the timer's Tick handler, check the Form's opacity; if it is less than 1.0, add 0.05 to it; otherwise, stop the timer. The reason you choose a Windows.Forms.Timer is that its events get handled by the main thread, and that is the only thread that is allowed to touch Controls and Forms. Other timers would tick on some threadpool thread, resulting in either some InvalidCrossThread exception, or the need for more code, based on Control.Invoke Also notice there is no explicit loop anywhere; the timer will keep firing until you tell it to stop (when opacity reached 1.0). :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups