Make labels slowly move into position to make an interesting "movie credit" effect in C#

(Note that the result in the executable is better than the result giving by the GIF on the right. The GIF is a bit jerky because it uses only 20 frames instead of the 200 used by the program.)

This example shows how to make labels slowly move into position, an effect sometimes used in movie credits.

The program displays all of its text in labels and moves the labels as necessary. When it starts, the program uses the following code to record some information about the labels.

// The Label controls we will
// animate and their properties.
private List

The program stores several pieces of information about the labels:

  • The labels themselves
  • The labels' starting positions
  • The distances (AnimateDxs and AnimateDys) the labels should move at each tick
  • The labels' current X and Y positions during animation (AnimateXs and AnimateYs)
  • The total number of ticks each label must move
  • The number of ticks remaining for a label to move in this animation

When you click the Animate button, the program executes the following code to prepare for the animations.

// Move the labels to the start positions and start animating them.
private void btnAnimate_Click(object sender, EventArgs e)
{
btnAnimate.Enabled = false;
AnimateTicksToGo = new List();
AnimateXs = new List();
AnimateYs = new List();

for (int i = 0; i < AnimateLabels.Count; i++)
{
AnimateXs.Add(AnimateStartXs[i]);
AnimateYs.Add(AnimateStartYs[i]);
AnimateLabels[i].Location =
new Point((int)AnimateXs[i], (int)AnimateYs[i]);
AnimateLabels[i].Visible = true;
AnimateTicksToGo.Add(AnimateTotalTicks[i]);
}

tmrMoveLabels.Enabled = true;
}

This code moves the labels to their start positions and resets the labels' AnimateTicksToGo values. It then enables the tmrMoveLabels timer. The following code shows that timer's Tick event handler.

// Move the labels.
private void tmrMoveLabels_Tick(object sender, EventArgs e)
{
bool done_moving = true;
DateTime now = DateTime.Now;
for (int i = 0; i < AnimateLabels.Count; i++)
{
if (AnimateTicksToGo[i]-- > 0)
{
done_moving = false;
AnimateXs[i] += AnimateDxs[i];
AnimateYs[i] += AnimateDys[i];
AnimateLabels[i].Location =
new Point((int)AnimateXs[i], (int)AnimateYs[i]);
}
}

// If all labels are done moving, disable the Timer.
if (done_moving)
{
tmrMoveLabels.Enabled = false;
tmrHideLabels.Enabled = true;
}
}

The Tick event handler examines each Label's AnimateTicksToGo. If a value is greater than zero (i.e. this Label needs to move again), the code adds the Dx and Dy value for that Label to its location. The code then decrements the AnimateTicksToGo value. (That's what the post decrement operator "--" after the variable means: the code examines it first and then decrements the value.)

After examining all of the labels, if none of the labels moved, the code disables the tmrMoveLabels timer and enables the tmrHideLabels timer. The new timer waits for 1 second and then hides the labels.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.