开发者

Make a simple animation in winforms using a loop

I need help making this loop to move a label move smoothly acr开发者_运维问答oss the screen using count for the animation (its required). The labels location is currently in location 0,0 I'd like to make it go in a square right -> down -> left -> back to its original position how can I accomplish this ? please give me an example using my code below. Thank You in advance.

    private void xAnimeTimer_Tick(object sender, EventArgs e)
    {
        int count;
        this.xAnimTimer.Stop();
        for (count = 0; count <= 100; count++)
        {
            this.xAnimLabel.Left = count;
        }

        for (count = 0; count <= 150; count++)
        {
            this.xAnimLabel.Top = count;
        }


I'm not sure why you have to use a for loop (which I believe is your problem [since it appears that each tick we loop completely first; hence why it doesn't draw 'cleanly']) but here's a stab at it before I've had my coffee this afternoon. Note that this probably doesn't satisfy what you want since you stated you needed a loop. Read the comments for the controls I'm using.

 public partial class Form1 : Form
{
    int left;
    int top;
    bool flgAllTheWayToTheRight = false;
    bool flgAllTheWayToTheBottom = false;

    public Form1()
    {
        // on my form I have two timers, named timer1 and tmrDraw
        // and then I have label1
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (left <= 100)
        { left++; }
        else
        { flgAllTheWayToTheRight = true; }

        if (flgAllTheWayToTheRight)
        {
            if (top <= 150)
            { top++; }
            else
            { flgAllTheWayToTheBottom = true; }
        }

        if (flgAllTheWayToTheBottom && flgAllTheWayToTheRight)
        {
            left = 0;
            top = 0;
            flgAllTheWayToTheRight = false;
            flgAllTheWayToTheBottom = false;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        label1.Text = "Animate Me.";
        timer1.Interval = 10;
        tmrDraw.Interval = 10;
        timer1.Start();
        tmrDraw.Start();
    }

    private void tmrDraw_Tick(object sender, EventArgs e)
    {
        label1.Left = left;
        label1.Top = top;
    }
}


Try

for (count = 0; count <= 100; count++)
{
    this.xAnimLabel.Left = count;
    Application.DoEvents(); // <-- New
    Thread.Sleep(100); // <-- New
}

This will make the label move visibly (the UI is redrawn) and more slowly. Then you'll see what's happening.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜