2009-05-05 7 views
2

Pouvez-vous me dire comment ajouter une animation gif à un bouton en C#. Juste en ajoutant gif aux ressources et le réglage comme l'image du bouton ne fonctionne pas très bien (les images suivantes apparaissent sur les précédentes). Le problème semble être de savoir comment C# traite la transparence mais je ne sais pas comment y remédier.Comment ajouter un gif animé à un bouton?

Merci d'avance.

--- --- modifier

Après avoir joué avec le gif la question était la transparence en gif. Le cadre suivant a été dessiné au-dessus du précédent donc les éléments que je voulais devenir transparents au milieu de l'animation ne l'ont pas fait correctement (ils ont toujours la couleur de l'image précédente). La solution consistait à créer des zones blanches sur l'image suivante pour couvrir la précédente. Le blanc était ma couleur transparente donc tout semblait bien après cela :)

J'espère que quelqu'un le verra utile. Bonne programmation :)

+1

Pourquoi faites-vous cela? –

+0

Le bouton est un bouton d'annulation - l'animation sur elle devrait indiquer la progression (elle doit juste se déplacer pour que l'utilisateur ne pense pas que le programme a planté). Le bouton est visible lorsque l'arrière-plan travaille et déclenche la méthode cancelasync. – kyrisu

+0

Vous ne voulez pas une barre de progression en mode chapiteau, alors? J'ai vu des applications qui utilisent le gif animé tournant (ou peu importe) et ils regardent presque invariablement b/c amateur ils ne s'ajustent pas correctement pour les thèmes d'OS plus récents. Une barre de progression sera. –

Répondre

1

Je l'ai eu pour travailler.

1 de 2 choses ont fonctionné pour moi:

  1. J'ai Réédité gif ajouter la propriété à chaque image s'undraw.

  2. J'ai changé la version de VS (j'utilise la version étudiante professionnelle gratuite, maintenant je suis à l'aide d'un abonnement MSDN de)

Merci pour les gars d'entrée. Il m'a guidé à la conclusion que cela pourrait être quelque chose de mal avec VS lui-même pas avec mon style de programmation :)

2

Pour ce faire, vous devez effectuer les opérations suivantes:

  1. Définissez la propriété BackgroundImageLayout au centre. Cette propriété est définie sur Mosaïque par défaut.

  2. Définissez la propriété Image du bouton sur votre GIF animé.

Cela fonctionnera, puisque je l'ai testé, et cela a fonctionné pour moi.

EDIT: Code Designer affiché ci-dessous:

this.Button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; 
this.Button4.Image = (System.Drawing.Image)resources.GetObject("Button4.Image"); 
this.Button4.Location = new System.Drawing.Point(220, 329); 
this.Button4.Name = "Button4"; 
this.Button4.Size = new System.Drawing.Size(81, 68); 
this.Button4.TabIndex = 4; 
this.Button4.Text = "Button4"; 
this.Button4.UseVisualStyleBackColor = true; 
+0

Malheureusement, cela n'a pas fonctionné pour moi. Peut-être que c'est la cause du format de gif (parce que quand j'ajoute la ressource sur l'aperçu, l'animation n'est pas comme elle devrait l'être, mais dans les navigateurs ça marche) – kyrisu

+0

Etrange ça ne marcherait pas pour toi. Si vous voulez poster le GIF en ligne quelque part, je peux jeter un oeil pour voir. – jgallant

+0

Heres le lien: http://wyslijto.pl/plik/b0p7cub92x Merci :) – kyrisu

2

Juste un mot pour tout le monde. L'animation Gif sera lue automatiquement si elle est utilisée sur la propriété .Image mais pas sur la propriété .BackGroundImage. Juste au cas où quelqu'un l'essaye de cette façon.

+0

ET dans le code de Jon: this.Button4.BackgroundImageLayout n'affecte pas la disposition de .Image ... parce que ce sont des propriétés bien différentes ... –

1
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Media; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     SoundPlayer sp = new SoundPlayer(@"1.wav"); 
     SoundPlayer s = new SoundPlayer(@"2.wav"); 

     private System.Windows.Forms.Label[] arLabel; 
     int temp = 10; 
     int help = 0; 
     int r = 0, b = 0, a, tem = 0; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      f1(); 
     } 

      private void arLabel_Click(object sender, System.EventArgs e) 
     { 
      Label myButton = (Label)sender; 


      if (myButton.Text == "") 
      { 

       myButton.ForeColor = System.Drawing.Color.Red; 



       sp.Play(); 
       temp = 1; 

       myButton.Text = "x".ToString(); 
       help = 0; 
       f3(); 
      } 
      for (int i = 0; i < arLabel.Length; i++) 
      { 
       if (arLabel[i].Text != "x") 
        this.arLabel[i].ForeColor = System.Drawing.Color.Green; 
      } 

      s.Play(); 
//------------------------------------------------------------------------------------------------------------------------------------ 

      if (temp == 1) 
      { 
       if (arLabel[6].Text == "" && arLabel[0].Text == "x" || arLabel[8].Text=="x"&&arLabel[6].Text=="") 
        arLabel[6].Text = "o".ToString(); 
       else if (arLabel[4].Text == "") 
        arLabel[4].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 

       else if (arLabel[0].Text == arLabel[1].Text && arLabel[2].Text == "")// 
        arLabel[2].Text = "o".ToString(); 
       else if (arLabel[0].Text == arLabel[2].Text && arLabel[1].Text == "")// 
        arLabel[1].Text = "o".ToString(); 
       else if (arLabel[1].Text == arLabel[2].Text && arLabel[0].Text == "")// 
        arLabel[0].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 
       else if (arLabel[6].Text == arLabel[7].Text && arLabel[8].Text == "") 
        arLabel[8].Text = "o".ToString(); 
       else if (arLabel[6].Text == arLabel[8].Text && arLabel[7].Text == "") 
        arLabel[7].Text = "o".ToString(); 
       else if (arLabel[8].Text == arLabel[7].Text && arLabel[6].Text == "") 
        arLabel[6].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 
       else if (arLabel[3].Text == arLabel[4].Text && arLabel[5].Text == "") 
        arLabel[5].Text = "o".ToString(); 
       else if (arLabel[3].Text == arLabel[5].Text && arLabel[4].Text == "") 
        arLabel[4].Text = "o".ToString(); 
       else if (arLabel[5].Text == arLabel[4].Text && arLabel[3].Text == "") 
        arLabel[3].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 
       else if (arLabel[0].Text == arLabel[3].Text && arLabel[6].Text == "") 
        arLabel[6].Text = "o".ToString(); 
       else if (arLabel[0].Text == arLabel[6].Text && arLabel[3].Text == "") 
        arLabel[3].Text = "o".ToString(); 
       else if (arLabel[6].Text == arLabel[3].Text && arLabel[0].Text == "") 
        arLabel[0].Text = "o".ToString(); 


//------------------------------------------------------------------------------------------------------------------------------------ 

       else if (arLabel[1].Text == arLabel[4].Text && arLabel[7].Text == "") 
        arLabel[7].Text = "o".ToString(); 
       else if (arLabel[1].Text == arLabel[7].Text && arLabel[4].Text == "") 
        arLabel[4].Text = "o".ToString(); 
       else if (arLabel[7].Text == arLabel[4].Text && arLabel[1].Text == "") 
        arLabel[1].Text = "o".ToString(); 
//------------------------------------------------------------------------------------------------------------------------------------ 
       else if (arLabel[2].Text == arLabel[5].Text && arLabel[8].Text == "") 
        arLabel[8].Text = "o".ToString(); 
       else if (arLabel[8].Text == arLabel[5].Text && arLabel[2].Text == "") 
        arLabel[2].Text = "o".ToString(); 
       else if (arLabel[2].Text == arLabel[8].Text && arLabel[5].Text == "") 
        arLabel[5].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 
       else if (arLabel[0].Text == arLabel[4].Text && arLabel[8].Text == "") 
        arLabel[8].Text = "o".ToString(); 
       else if (arLabel[0].Text == arLabel[8].Text && arLabel[4].Text == "") 
        arLabel[4].Text = "o".ToString(); 
       else if (arLabel[8].Text == arLabel[4].Text && arLabel[0].Text == "") 
        arLabel[0].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 
       else if (arLabel[2].Text == arLabel[4].Text && arLabel[6].Text == "") 
        arLabel[6].Text = "o".ToString(); 
       else if (arLabel[2].Text == arLabel[6].Text && arLabel[4].Text == "") 
        arLabel[4].Text = "o".ToString(); 
       else if (arLabel[4].Text == arLabel[6].Text && arLabel[2].Text == "") 
        arLabel[2].Text = "o".ToString(); 
       //------------------------------------------------------------------------------------------------------------------------------------ 
       else 
        for (int i = 0; i < 9; i++) 
        { 
         if (arLabel[8].Text == "") 
         { 
          arLabel[8].Text = "o".ToString(); 
          break; 
         } 
         else if (arLabel[7].Text == "") 
         { 
          arLabel[7].Text = "o".ToString(); 
          break; 
         } 
         else if (arLabel[6].Text == "") 
         { 
          arLabel[6].Text = "o".ToString(); 
          break; 
         } 

//------------------------------------------------------------------------------------------------------------------------------------ 
         else if (arLabel[i].Text == "") 
         { 
          this.arLabel[i].ForeColor = System.Drawing.Color.Red; 
          arLabel[i].Text = "o"; 
          break; 
         } 

        } 

      } 
      temp = 0; 
      help = 1; 
      f3(); 



     } 










     private void f1() 
     { 
      this.arLabel = new Label[9]; 
      int b = 400; 
      int c = 40; 
      for (int i = 0; i < arLabel.Length; i++) 
      { 
       if (i == 3 || i == 6) 
       { 
        c = c + 200; 
        b =400; 
       } 
       this.arLabel[i] = new Label(); 
       this.arLabel[i].Location = new System.Drawing.Point(b, c); 
       this.arLabel[i].Size = new System.Drawing.Size(200, 200); 
       this.arLabel[i].TabIndex = i; 
       this.arLabel[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
       this.arLabel[i].Text = "".ToString(); 
       arLabel[i].Click += new System.EventHandler(this.arLabel_Click); 
       this.Controls.Add(arLabel[i]); 
       this.arLabel[i].Font = new System.Drawing.Font("Modern No. 20", 120.74999F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
       this.arLabel[i].ForeColor = System.Drawing.Color.Green; 
       arLabel[i].Visible = true; 
       this.arLabel[i].BackColor = System.Drawing.Color.Khaki; 
       this.arLabel[i].BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
       b = b + 200; 
      } 
     } 
     private void f3() 
     { 
      if (arLabel[0].Text == arLabel[1].Text && arLabel[2].Text == arLabel[1].Text && arLabel[1].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
      if (arLabel[3].Text == arLabel[4].Text && arLabel[3].Text == arLabel[5].Text && arLabel[3].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
      if (arLabel[6].Text == arLabel[7].Text && arLabel[8].Text == arLabel[6].Text && arLabel[6].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
      // if (arLabel[0].Text == arLabel[3].Text && arLabel[0].Text == arLabel[6].Text && arLabel[6].Text != "".ToString()) 
      // f2(); 
      if (arLabel[0].Text == arLabel[3].Text && arLabel[0].Text == arLabel[6].Text && arLabel[6].Text != "".ToString()) 
      { 
       f2(); 
       temp = 0; 
      } 
      if (arLabel[1].Text == arLabel[4].Text && arLabel[4].Text == arLabel[7].Text && arLabel[7].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
      if (arLabel[2].Text == arLabel[5].Text && arLabel[8].Text == arLabel[2].Text && arLabel[2].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
      if (arLabel[0].Text == arLabel[4].Text && arLabel[4].Text == arLabel[8].Text && arLabel[8].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
      if (arLabel[2].Text == arLabel[4].Text && arLabel[6].Text == arLabel[4].Text && arLabel[4].Text != "".ToString()) 
      { 
       temp = 0; 
       f2(); 
      } 
     } 
     private void f2() 
     { 
      if (help == 1) 
      { 

       r++; 
       // label4.Text = r.ToString(); 
       MessageBox.Show("THE is o"); 
       f6(); 
      } 
      else 
      { 

       b++; 

       // label5.Text = b.ToString(); 
       MessageBox.Show("THE is x"); 
      } 


      for (int i = 0; i < arLabel.Length; i++) 

       arLabel[i].Text = "".ToString(); 
      f6(); 
     } 
     private void f6() 
     { 
      Random number = new Random(); 
      a = number.Next(0, 8); 
      this.arLabel[a].ForeColor = System.Drawing.Color.Green; 
      arLabel[a].Text = "o".ToString(); 

      s.PlaySync(); 

     } 
    } 


} 
Questions connexes