2009-04-28 77 views
4

我想在TabPage上顯示一個動畫GIF作爲標籤圖像。ImageList/TabPage中的動畫GIF

如果我將圖像添加到一個ImageList,並使用TabPage.ImageIndex屬性,它只能顯示第一幀(和不動畫):

ImageList imageList = new ImageList(); 
imageList.Images.Add(Properties.Resources.my_animated_gif); 

tabControl.ImageList = imageList; 
tabPage.ImageIndex = 0; 

在網上的一些論壇也建議ImageList沒有按」 t支持動畫GIF。

有沒有一種簡單的方法在TabPage上顯示動畫GIF作爲圖像?我是否必須自己繪製圖像並對其進行動畫處理?

回答

1

這是一個遲到的答案,但希望有人會從中受益,下面是我在TabPage中爲圖像製作動畫所做的動作,我用它來顯示動畫加載圖標,假設您已經提取了GIF並將其納入資源中。

using System; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Threading; 
namespace GuiLib 
{ 

public class AnimatedTabControl : TabControl 
{ 

    static int ITEM_WIDTH = 250; 
    static int ITEM_HEIGHT = 25; 
    static int TIMER_INTERVAL = 80; 

    static int ICON_X = 3; 
    static int ICON_Y = 3; 
    static int ICON_WIDTH = 15; 
    static int ICON_HIGHT = 15; 

    static int TEXT_X = 50; 
    static int TEXT_Y = 6; 
    static int TEXT_WIDTH = 200; 
    static int TEXT_HIGHT = 15; 

    int animationIndex; 
    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); 

    Bitmap[] animationImages = {new Bitmap(GuiLib.Properties.Resources._0), new Bitmap(GuiLib.Properties.Resources._1),new Bitmap(GuiLib.Properties.Resources._2), 
           new Bitmap(GuiLib.Properties.Resources._3),new Bitmap(GuiLib.Properties.Resources._4),new Bitmap(GuiLib.Properties.Resources._5), 
           new Bitmap(GuiLib.Properties.Resources._6),new Bitmap(GuiLib.Properties.Resources._7)}; 
    Bitmap animatedimage; 

    public AnimatedTabControl() 
     : base() 
    {    
     this.DrawMode = TabDrawMode.OwnerDrawFixed; 
     this.SizeMode = TabSizeMode.Fixed; 
     this.ItemSize = new Size(ITEM_WIDTH, ITEM_HEIGHT); 
     myTimer.Tick += new EventHandler(TimerEventProcessor); 
     myTimer.Interval = TIMER_INTERVAL; 
     animationIndex = 0; 
    } 

    private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) 
    { 
     animationIndex++; 

     if (animationIndex >= animationImages.Length) 
      animationIndex = 0; 

     animatedimage = animationImages[animationIndex]; 
     AnimateLoadingTabsOrStopIfNonIsLoading(); 
    } 

    private void AnimateLoadingTabsOrStopIfNonIsLoading() 
    { 
     bool stopRunning = true; 
     for (int i = 0; i < this.TabPages.Count; i++) 
     { 
      if (this.TabPages[i] is LoadingTabPage) 
      { 
       LoadingTabPage ltp = (LoadingTabPage)this.TabPages[i]; 

       if (ltp.Loading) 
       { 
        stopRunning = false; 
        Rectangle r = GetTabRect(i); 
        this.Invalidate(new Rectangle(r.X + ICON_X, r.Y + ICON_Y, ICON_WIDTH, ICON_HIGHT)); 
       } 
      } 
     } 

     if (stopRunning) 
      myTimer.Stop(); 
    } 

    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     Rectangle r = e.Bounds; 
     r = GetTabRect(e.Index); 

     DrawAnimationImageIfLoading(e, r); 
     DrawTabTitle(e, r); 
    } 

    private void DrawTabTitle(DrawItemEventArgs e, Rectangle r) 
    { 
     string title = this.TabPages[e.Index].Text; 
     StringFormat titleFormat = new StringFormat(); 
     titleFormat.Trimming = StringTrimming.EllipsisCharacter; 
     e.Graphics.DrawString(title, this.Font, Brushes.Black, new RectangleF(r.X + TEXT_X, r.Y + TEXT_Y, TEXT_WIDTH, TEXT_HIGHT), titleFormat); 
    } 

    private void DrawAnimationImageIfLoading(DrawItemEventArgs e, Rectangle r) 
    { 
     if (this.TabPages[e.Index] is LoadingTabPage) 
     { 
      if (((LoadingTabPage)this.TabPages[e.Index]).Loading) 
      { 
       if (animatedimage != null) 
        e.Graphics.DrawImage(animatedimage, new RectangleF(r.X + ICON_X, r.Y + ICON_Y, ICON_WIDTH, ICON_HIGHT)); 

       if (!myTimer.Enabled) 
        myTimer.Start(); 
      } 
     } 
    }  
} 
} 

而且LoadingTabPage就象這樣:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace GuiLib 
{ 
/// <summary> 
/// A Class to facilitate tab page with animated loading icon. 
/// </summary> 
public class LoadingTabPage : TabPage 
{ 
    public LoadingTabPage(string s) 
     : base(s) 
    { 
     loading = false; 
    } 

    public LoadingTabPage() 
     : base() 
    { 
     loading = false; 
    } 

    private bool loading; 

    public bool Loading 
    { 
     get { return loading; } 
     set 
     { 
      loading = value; 
      if (this.Parent != null) 
      { 
       this.Parent.Invalidate(); 
      } 
     } 
    } 

} 

} 

的使用會很容易:

myLoadingTabPage.Loading = true; 
+0

因爲我認爲,唯一的解決辦法是所有者繪製。但是,獲得一些參考代碼是很好的,我希望它可以幫助未來的人。 – 2015-05-19 01:48:05

1

我推薦(免費的解決方案)使用後臺工作人員在循環中以編程方式更新圖標(並檢查天氣停止或繼續)。這有點複雜,但我認爲你明白了嗎? = P