Fading a Form
-
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!
-
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!
Uses a timer and increase the opacity until the form is fully opaque.
Philippe Mori
-
Uses a timer and increase the opacity until the form is fully opaque.
Philippe Mori
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.
-
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.
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