2011-12-29 94 views
0

我有目前2個圖像之間的切換正常和鼠標狀態上的類,但我想補充的是,2IE 9等Winforms中按鈕熱跟蹤

之間切換的微妙的動畫是否有任何(簡單)的方式來讓一個按鈕具有動畫效果,例如IE 9瀏覽器中的按鈕。

編輯:

這裏是我通過joe_coolish

protected override void OnPaint(PaintEventArgs e) 
    { 
     e.Graphics.DrawImage(_normalImage, 0, 0); 

     ColorMatrix matrix = new ColorMatrix(); 
     matrix.Matrix33 = _opacity; 

     ImageAttributes attributes = new ImageAttributes(); 
     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 

     e.Graphics.DrawImage(_hotImage, this.DisplayRectangle, 0, 0, 
      this.DisplayRectangle.Width, this.DisplayRectangle.Height, GraphicsUnit.Pixel, attributes); 

     if (_opacity < 1 && _over) 
     { 
      _opacity += 0.02f; 
      _opacity = Math.Min(1, _opacity); 
      this.Invalidate(); 
     } 
     if (_opacity > 0 && !_over) 
     { 
      _opacity -= 0.02f; 
      _opacity = Math.Max(0, _opacity); 
      this.Invalidate(); 
     } 
    } 

使用的OnPaint過載感謝答案最終溶液我結束了一種能夠防止不透明度從上述1或低於設定0,否則我最終會遇到一些奇怪的行爲。

注意:確保控制是雙緩衝或否則閃爍不好也很重要。

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
+0

改變焦點或鼠標上的圖像是否有意義?我錯過了什麼嗎? – 2011-12-29 03:26:48

+0

我希望控件在鼠標懸停時更改圖像,並在鼠標離開時恢復正常。我的問題不是關於狀態如何/何時改變,而是關於如何在狀態發生變化時對其進行動畫處理。 – ChandlerPelhams 2011-12-29 03:51:08

+0

我的解決方案適合您嗎? – 2011-12-29 15:28:52

回答

2

圖像有何不同?如果你想要一個「淡入/淡出」式的東西,嘗試是這樣的:

private float _opacity = 0.0f; 
    private bool _over = false; 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     e.Graphics.DrawImage(imgOne); 

     ColorMatrix matrix = new ColorMatrix(); 
     matrix.Matrix33 = _opacity; //opacity 0 = completely transparent, 1 = completely opaque 

     ImageAttributes attributes = new ImageAttributes(); 
     attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 

     e.Graphics.DrawImage(imgTwo, new Rectangle(0, 0, imgTwo.Width, imgTwo.Height), 0, 0, imgTwo.Width, imgTwo.Height, GraphicsUnit.Pixel, attributes); 

     if(_opacity < 1 && _over) 
     { 
      _opacity += 0.05f; // Play with this!! 
      Invalidate(); 
     } 
     if(_opacity > 0 && !_over) 
     { 
      _opacity -= 0.05f; // Play with this!! 
      Invalidate();  
     } 
    } 

然後就是設置_over BOOL當鼠標進入和退出!

這是一個非常粗糙的代碼集,如果它甚至可以編譯的話,它就是IDK,但這是一個非常好的起點!

+0

圖像幾乎相同,只是不同的顏色 - 我嘗試使用您的代碼,但它似乎並不影響圖像之間的轉換,而不是簡單地切換它們 – ChandlerPelhams 2011-12-30 03:16:32

+0

降低+ =/- =值。應該淡入淡出。嘗試像0.01。如果這不起作用,讓我知道,我會更多地調查它:) – 2011-12-30 14:29:12

+0

我最終使用.02來給出一個很好的微妙淡入淡出,這正是我所期待的。感謝解決方案,它完美運作! – ChandlerPelhams 2011-12-30 17:38:19