【问题标题】:How to create animated glow effect on Button Hover with C#?如何使用 C# 在 Button Hover 上创建动画发光效果?
【发布时间】:2014-03-03 13:41:14
【问题描述】:

我正在为我的应用程序创建一个 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 窗体控件

【问题讨论】:

  • 为此我强烈推荐 WPF。 winforms 不支持动画、效果或类似的东西。
  • 我的应用程序是一个带有 .net framework 2.0 的 Windows 窗体应用程序,我无法将其转换为 WPF 应用程序。
  • @HighCore 我同意你的观点,winform 不支持这个。但这并不意味着我们必须妥协;如果我们花时间,我们可以做到。
  • @HighCore 对获胜形式的一些不必要的苛刻。它可能不像 WPF 那样现代,而且根本不是为 Web 项目设计的,但是使用 GDI+ 进行绘图是快速且用途广泛的。在某些情况下,您必须覆盖 OnPaintBackground 以防止闪烁,但这不是“硬件加速”问题。
  • 很酷的东西@HighCore :-)。毫无疑问,WPF 支持一些很酷的 GUI 动画,并且可能具有 GDI+ 无法跟上的效果。但是,如果您需要进行严格的 CAD 或 GIS 渲染,加载具有数万个坐标的 WPF 几何图形将导致您的应用程序基本上停止运行,而 GDI+ 可以相对轻松地渲染相同的 2D 形状。此外,开发人员通常无法控制他们要使用的技术,因此仅仅因为您在 Forms 中进行开发而完全放弃并不是一种有用的方法。

标签: c# winforms animation


【解决方案1】:

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

在 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; 
}

这是我在项目中通常会做的事情。

【讨论】:

  • 你是如何制作动画的?我想让它像淡入淡出效果一样动画。请阅读我已经提到的问题。
  • 你能给我们看看有和没有发光效果的按钮图像吗?
  • 我没有使用任何图像来创建按钮。我正在使用图形对象绘制它。因为,每个按钮对象的按钮主题应该是不同的。
【解决方案2】:

这是一些使用计时器并覆盖 OnPaint 方法的代码。它跳过了 10 而不是 1,因为我担心你不会足够快地看到效果。 Timer 间隔以毫秒为单位并设置为 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




    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2011-07-01
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多