2) анимация выполняется безостановочно столько, сколько выполняется наше приложение, но анимационный объект изменяется не за каждый интервал времени Interval, а через заданное нами число интервалов N_Interval ;
3) анимация выполняется столько времени, какое число интервалов времени мы задали, и после этого времени анимационный объект останавливается в заданном нами положении.
Сначала изучим первый вариант, когда анимация выполняется безостановочно за каждый интервал времени Interval. Для этого дважды щёлкаем значок timer1 (рис. 2.8) в режиме проектирования (или в панели Properties на вкладке Events дважды щёлкаем по имени единственного события Tick).
Появляется файл Form1.cs с автоматически сгенерированным шаблоном метода, выше которого объявляем булеву переменную, а в шаблон записываем такой код.
Листинг 2.3. Код для создания анимации. Вариант 1.
//We declare the Boolean myText variable and appropriate it
//”false” (by default too "false"):
bool myText = false;
private void timer1_Tick(object sender, EventArgs e)
{
//We set the alternation of " Calculator " and
//" Calculator with animation ":
if (myText == false)
{
this.Text = " Calculator ";
//We change the myText value to opposite:
myText = true; //or so: myText =! myText;
}
else
{
this.Text = " Calculator with animation ";
//We change the myText value to opposite:
myText = false; //or so: myText =! myText;
}
}
В этом коде использовано правило переноса текста с одной строки на другую.
Теперь изучим второй вариант, когда анимация выполняется безостановочно столько, сколько выполняется наше приложение, но анимационный объект изменяется не за каждый интервал времени Interval, а через заданное нами число интервалов N_Interval. Для этого в файле Form1.cs выше того же (что и в предыдущем варианте) автоматически сгенерированного шаблона метода объявляем переменные, а в шаблон записываем код, как показано на следующем листинге.
Листинг 2.4. Код для создания анимации. Вариант 2.
//We declare the counter of number of intervals
//and set its initial value:
private int counter = 0; //by default too it is equal to 0.
//We declare and set the number N_Interval of intervals,
//after which one text is replaced by another:
private int N_Interval = 3;
private void timer1_Tick(object sender, EventArgs e)
{
//We check, value of the counter
//equally or yet is not present to number N_Interval of
//intervals,
//after which one text is replaced by another:
if (counter >= N_Interval)
{
//If value of the counter collected and is equal
//N_Interval, we output other text:
this.Text = "Calculator";
//We nullify the value of the counter again:
counter = 0;
}
else
{
//If value of the counter did not collect yet
//and N_Interval is not equal,
//that we output the first text:
this.Text = "Calculator with animation";
//We increase value of the counter on 1:
counter = counter + 1;
}
}
И наконец, изучим третий вариант, когда анимация выполняется столько времени, какое число интервалов времени N_Interval_Stop мы задали, и после этого времени анимационный объект останавливается в заданном нами положении. Для этого в файле Form1.cs выше того же (что и в предыдущем варианте) автоматически сгенерированного шаблона метода объявляем большее число переменных, а в шаблон записываем более общий код, как показано на следующем листинге.
Листинг 2.5. Код для создания анимации. Вариант 3.
//We declare the counter of number of intervals
//and set its initial value:
private int counter = 0;
//We declare and set the number N_Interval of intervals,
//after which one text is replaced by another:
private int N_Interval = 3;
//We declare and nullify the i_Interval_Stop counter,
//which calculates the number of intervals
//to an animation stop:
private int i_Interval_Stop = 0;
//We declare and set the number N_Interval_Stop of intervals,
//on reaching which animation stops:
private int N_Interval_Stop = 10;
private void timer1_Tick(object sender, EventArgs e)
{
//Value of the i_Interval_Stop counter,
//which calculates the number of intervals
//to an animation stop, we increase on 1:
i_Interval_Stop = i_Interval_Stop + 1;
//We check the number i_Interval_Stop of intervals,
//on reaching which animation stops:
if (i_Interval_Stop >= N_Interval_Stop)
timer1.Enabled = false;
//We check, value of the counter
//equally or yet is not present to number N_Interval of
//intervals,
//after which one text is replaced by another:
if (counter >= N_Interval)
{
//If value of the counter collected and is equal
//N_Interval, we output other text:
this.Text = "Calculator";
//We nullify value of the counter again:
counter = 0;
}
else
{
//If value of the counter did not collect yet
//and N_Interval is not equal,
//that we output the first text:
this.Text = "Calculator with animation";
//We increase value of the counter on 1:
counter = counter + 1;
}
}
Так как здесь мы впервые применили метод timer1_Tick, а далее постоянно будем его применять, то дадим краткие пояснения.
Автоматически сгенерированный заголовок метода
private void timer1_Tick(object sender, EventArgs e)