2014-03-03 25 views
1

我正在为我的应用程序创建一个CustomButton控件。现在,我想要做的是当鼠标移动到按钮上时它应该显示发光效果,当鼠标离开时它应该恢复正常。但不应该立即显示发光效果。它应该显示动画。就像Chrome浏览器标签页。我在按钮控件中试过这个逻辑。如何使用C#在Button Hover上创建动画发光效果?

这是我的逻辑。但是,我认为这不是一个正确的方法。请建议适当的方式来获得发光效果。

private void ShowGlow() 
{ 
    for (int i = 0; i<50; i+= 5) 
    { 
     Sleep(100); 
     Graphics g = this.CreateGraphics(); 
     Color color = Color.FromArgb(i, 150,150,25); 
     g.FillRectangle(new SolidBrush(color), this.ClientRectangle); 
    } 
} 

其他细节 的Visual Studio 2005,Windows XP和Windows窗体控件

+0

我强烈建议WPF为此。 winforms不支持动画,或效果,或类似的东西。 –

+0

我的应用程序是.net框架2.0的Windows窗体应用程序,我无法将其转换为WPF应用程序。 – Shell

+1

@HighCore我同意你的观点,winform不支持​​这一点。但这并不意味着我们必须妥协;如果我们花时间,我们可以做到这一点。 –

回答

3

我建议你一个简单的方法。 创建两个图像,发光效果和没有。 并使用此代码。

上的MouseEnter:

private void MyButton_MouseEnter(object sender, EventArgs e) 
{ 
    MyButton.BackgroundImage = Properties.Resources.WithGlow; 
} 

在鼠标离开:

private void MyButton_MouseLeave(object sender, EventArgs e) 
{ 
    MyButton.BackgroundImage = Properties.Resources.WithoutGlow; 
} 

这是我平时在我的项目做。

+0

你如何使它动画?我想让它动画就像淡入淡出效果一样。请阅读我已经提到过的问题。 – Shell

+0

你能向我们展示带有和没有发光效果的按钮图像吗? –

+1

我没有使用任何图像来创建按钮。我正在使用图形对象绘制它。因为每个按钮对象的按钮主题应该不同。 – Shell

3

这是一些使用计时器并重写OnPaint方法的代码。它跳过10而不是1,因为我害怕你不会以足够快的速度看到效果。计时器时间间隔以毫秒为单位并被设置为100,因为这是您在原始示例中用于睡眠的原因。如果您需要更快的效果,您可以缩短间隔时间。如果它应该更慢,您可以增加间隔,或者减少每次打勾增加α的次数。

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace LicensePlate 
{ 
    /// <summary> 
    /// The GlowButton class 
    /// </summary> 
    public class GlowButton : Button 
    { 
     #region Fields 
     Timer timer; 
     private int alpha; 
     Color color; 

     #endregion 

     #region Events 

     #endregion 

     #region Constructor 


     /// <summary> 
     /// Creates a new instance of the GlowButton class. 
     /// </summary> 
     public GlowButton() 
     { 
      timer = new Timer(); 
      timer.Interval = 100; 
      timer.Tick += timer_Tick; 
     } 


     #endregion 

     #region Methods 

     /// <summary> 
     /// Only used if you need something else to trigger the glow process 
     /// </summary> 
     private void ShowGlow() 
     { 
      timer.Start(); 
     } 

     /// <summary> 
     /// Start the timer and reset glow if the mouse enters 
     /// </summary> 
     /// <param name="e"></param> 
     protected override void OnMouseEnter(EventArgs e) 
     { 
      timer.Start(); 
      alpha = 0; 
     } 


     /// <summary> 
     /// Reset the glow when the mouse leaves 
     /// </summary> 
     /// <param name="e"></param> 
     protected override void OnMouseLeave(EventArgs e) 
     { 
      timer.Stop(); 
      alpha = 0; 
      color = BackColor; 
      Invalidate(); 
     } 


     /// <summary> 
     /// Override paint so that it uses your glow regardless of when it is instructed to draw 
     /// </summary> 
     /// <param name="pevent"></param> 
     protected override void OnPaint(PaintEventArgs pevent) 
     { 
      base.OnPaint(pevent); 
      if (alpha > 0) 
      { 
       using (Brush b = new SolidBrush(color)) 
       { 
        pevent.Graphics.FillRectangle(b, this.ClientRectangle); 
       } 
      } 

      //base.OnPaint(pevent); 
     } 

     /// <summary> 
     /// Use a timer tick to set the color and increment alpha 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     void timer_Tick(object sender, EventArgs e) 
     { 
      alpha+=10; 
      color = Color.FromArgb(alpha, 150, 150, 25); 
      if (alpha > 50) { 
       timer.Stop(); 
      } 

      Invalidate(); 
     } 

     #endregion 




    } 
} 
相关问题