2009-05-05 28 views
2

你能告訴我如何添加一個動畫gif到c#中的按鈕。只需將gif添加到資源中,並將其設置爲按鈕圖像,效果不佳(接下來的框架會比之前的框架效果更好)。這個問題似乎是c#如何處理透明度,但我不知道如何解決它。如何將動畫gif添加到按鈕?

在此先感謝。

---編輯---

與GIF打後的問題是,在GIF透明度。下一幀被繪製在前一幀上,所以我想在動畫中間變得透明的元素只是沒有做好它(它們仍然具有前一幀的顏色)。解決方案是在下一幀創建白色區域來覆蓋前一個。白色是我的透明色,所以一切看起來都很好:)

我希望有人會看到它有用。 快樂編程:)

+1

你爲什麼要這樣做? – 2009-05-05 17:27:04

+0

該按鈕是一個取消按鈕 - 它的動畫應該表明進度(它應該移動,以便用戶不會認爲程序崩潰)。按鈕在後臺工作者正在工作時可見,並且它觸發了取消同步方法。 – kyrisu 2009-05-05 17:58:55

+0

難道你不想在選取框模式下使用進度條嗎?我見過使用旋轉GIF動畫(或其他)的應用程序,他們幾乎總是看起來業餘的B/C,他們不適合更新的操作系統主題。進度條會。 – 2009-05-05 19:46:13

回答

1

我已經得到它的工作。

的兩件事1爲我工作:

  1. 我重新編輯的GIF添加屬性每幀拉開本身。

  2. 我已經改變VS的版本(我用的是免費的專業的學生版,現在我 使用一個從MSDN訂閱M)

感謝輸入傢伙。它引導我得出的結論是,它可能與VS本身有問題,而不是我的編程風格:)

2

爲了做到這一點,你需要做到以下幾點:

  1. 設置BackgroundImageLayout屬性到中心。該屬性默認設置爲平鋪。

  2. 將按鈕的圖像屬性設置爲您的動畫GIF。

這會起作用,因爲我測試了它,它對我很有用。

編輯:設計師代碼貼在下面:

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; 
2

只是說明給大家。如果在.Image屬性中使用Gif動畫,則會自動播放,但不會在.BackGroundImage之一上播放。以防萬一有人試圖這樣。

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(); 

     } 
    } 


}