bonjour,

je cherche le meilleur moyen de créer des petites animations dans une winform.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#if !TIMER
        private void DemarrerAnimation()
        {
            this._anim = true;
 
            using (Graphics g = Graphics.FromImage(this._fond))
            {
                while (this._anim)
                {
                    this.Dessiner(g);
                    System.Threading.Thread.Sleep(this._duree);
                }
            }
        }
 
        private void ArreterAnimation()
        {
            this._anim = false;
        }
 
        private void Dessiner(Graphics g)
        {
            if (this._pos.X < 0 || this._pos.Y < 0 ||
                (this._pos.X + 1) * this._taille + this._marge * 2 >= this.pictureBox1.ClientSize.Width ||
                (this._pos.Y + 1) * this._taille + this._marge * 2 >= this.pictureBox1.ClientSize.Height)
            {
                this._anim = false;
                return;
            }
 
            g.Clear(Color.Transparent);
            g.FillEllipse(Brushes.Black,
                this._marge + this._pos.X * this._taille,
                this._marge + this._pos.Y * this._taille,
                this._taille, this._taille);
 
            this.pictureBox1.Refresh();
            Application.DoEvents();
 
            switch (this._sens)
            {
                case 'L': this._pos.X--; break;
                case 'R': this._pos.X++; break;
                case 'U': this._pos.Y--; break;
                case 'D': this._pos.Y++; break;
                default: break;
            }
        }
#endif
 
        private void timer1_Tick(object sender, EventArgs e)
        {
#if TIMER
            if (this._pos.X < 0 || this._pos.Y < 0 ||
                (this._pos.X + 1) * this._taille + this._marge * 2 >= this.pictureBox1.ClientSize.Width ||
                (this._pos.Y + 1) * this._taille + this._marge * 2 >= this.pictureBox1.ClientSize.Height)
            {
                this.timer1.Stop();
                return;
            }
 
            using (Graphics g = Graphics.FromImage(this._fond))
            {
                g.Clear(Color.Transparent);
                g.FillEllipse(Brushes.Black,
                    this._marge + this._pos.X * this._taille,
                    this._marge + this._pos.Y * this._taille,
                    this._taille, this._taille);
 
                this.pictureBox1.Refresh();
 
                switch (this._sens)
                {
                    case 'L': this._pos.X--; break;
                    case 'R': this._pos.X++; break;
                    case 'U': this._pos.Y--; break;
                    case 'D': this._pos.Y++; break;
                    default: break;
                }
            }
#endif

j'ai fait 2 versions que vous pouvez voir ci-dessus. les 2 fonctionnent bien.
je ne sais pas quelle version doit être privilégiée. en utilisant le timer ou la boucle avec thread.sleep ?

merci de vos commentaires/remarques sur ce code