如何使用C#在Button Hover上创建动画发光效果?

时间:2014-03-03 04:49:57

标签: c# winforms animation

我正在为我的应用程序创建 CustomButton 控件。现在,当鼠标按下按钮时,我想要做的是它应该显示发光效果,当鼠标离开它应该恢复正常。但不应立即显示发光效果。它应该显示动画。就像 Chrome浏览器标签页一样。我在按钮控件中尝试了这个逻辑。

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

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窗体控件

2 个答案:

答案 0 :(得分:3)

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

在MouseEnter上:

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

在MouseLeave上:

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

这就是我在项目中经常做的事情。

答案 1 :(得分:3)

以下是一些使用计时器并覆盖OnPaint方法的代码。它跳过了10而不是1,因为我害怕你不会快速看到效果。计时器间隔以毫秒为单位,并设置为100,因为这是您在原始睡眠示例中使用的。如果需要效果更快,可以缩短间隔。如果它应该更慢,你可以增加间隔,或减少每次滴答增加alpha的数量。

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




    }
}
相关问题