Author: Liu Wenbo please indicate the source
In the process of programming, we often encounter such problems. You need to show a state in the form of an animation at some time. Typical applications, such as downloading files, are connecting to servers, and more. One of our solutions is to form an animation effect by continuously drawing different static pictures on the interface, usually triggering the drawing function in the timer.
The following is an implementation method I have ever seen, using a global variable (ndownloadcount) to control the four position overlapping window control alternate display, each control is actually a picture, below the code snippet:
Case WM_TIMER:
IF (NDOWNLOADTIMERUSED && IDEVENT == TimerDownloadID)
{
IF (ndownloadcount == 0)
{
ShowWindow (hstate [0], sw_show);
ShowWindow (HState [1], SW_HIDE);
ShowWindow (HState [2], SW_HIDE);
ShowWindow (HState [3], SW_HIDE);
NDOWNLOADCOUNT = 1;
}
Else IF (ndownloadcount == 1)
{
ShowWindow (hstate [0], sw_hide);
ShowWindow (HState [1], SW_SHOW);
ShowWindow (HState [2], SW_HIDE);
ShowWindow (HState [3], SW_HIDE);
NDOWNLOADCOUNT = 2;
}
Else IF (ndownloadcount == 2)
{
ShowWindow (hstate [0], sw_hide);
ShowWindow (HState [1], SW_HIDE);
ShowWindow (HState [2], SW_SHOW);
ShowWindow (HState [3], SW_HIDE);
NDOWNLOADCOUNT = 3;
}
Else IF (ndownloadcount == 3)
{
ShowWindow (hstate [0], sw_hide);
ShowWindow (HState [1], SW_HIDE);
ShowWindow (HState [2], SW_HIDE);
ShowWindow (HState [3], SW_SHOW);
NDOWNLOADCOUNT = 0;
}
}
Break;
My day, such a big piece of code adds a global variable that makes a coupling, just to achieve a simple animation display, the function is implemented, but always feel awkward.
Is there a better way? I tried to use the following method:
Case WM_TIMER:
IF (NDOWNLOADTIMERUSED && IDEVENT == TimerDownloadID)
{
Static unsigned int ndownloadcount = 0;
ShowWindow (HState [NDOWNLOADCOUNT % 4], SW_HIDE);
ShowWindow (HState [ndownloadcoun], sw_show);
}
Break;
Haha, the effect is very good. With a static partial variable, add two lines of code, whether you are a few pictures, you can use this mode to solve it, just change the digital to the model. Maybe you say this is really, but my picture is too fast, for example, as long as half of the speed is only, what should I do? It is said that it is changed as follows: static unsigned int ndownloadcount = 0;
Static unsigned int NTImerPercycle = 0;
IF (NtimerCycle% 2 == 0)
{
ShowWindow (HState [NDOWNLOADCOUNT % 4], SW_HIDE);
ShowWindow (HState [ndownloadcoun], sw_show);
}
It is simple. No global variables are required, so that the degree of coupling of the module is relatively low, the internal polymetidity is relatively high, and it is very simple and efficient. You may worry that the static variable has been increased, will not cause data overflow, my answer is not, when it reaches the maximum value, I will return to 0.