如何在鼠标点击时制作一个闪烁/淡出的控件?(Windows)

3
当用户在我的控件的某些位置单击时,我想要更改网格中一些行和列的颜色,然后在大约500毫秒内淡化回正常颜色。 我还没有决定使用Winforms还是WPF,因此任何一种技术的建议都可以。谢谢。
编辑:我知道我可以通过在单击事件中调用Paint并正确设置绘制参数来实现这一点。 但是我相信那会阻塞UI,而我希望比那更具响应性。
3个回答

2

WPF对动画有很好的支持。从xaml和代码后台都可以支持动画,因此您应该能够实现任何您想要的外观。

MSDN WPF动画概述 看起来提供了很多有用的信息,可以帮助您入门。


+WPF对动画的支持+1。比Winforms中的多线程简单多了。 - chakrit
动画确实很棒。但是因为它们很快变得冗长,没有 Blend 我就迷失了。 - Artur Carvalho

1

这里有一种处理淡入淡出的方法:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public class FadeForm : Form
    {
        private Timer fadeTimer;
        private Panel fadePanel;
        private Button fadeButton;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose( bool disposing )
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fadePanel = new System.Windows.Forms.Panel();
            this.fadeButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // fadePanel
            // 
            this.fadePanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.fadePanel.Location = new System.Drawing.Point( 4, 8 );
            this.fadePanel.Name = "fadePanel";
            this.fadePanel.Size = new System.Drawing.Size( 276, 104 );
            this.fadePanel.TabIndex = 0;
            // 
            // fadeButton
            // 
            this.fadeButton.Location = new System.Drawing.Point( 104, 116 );
            this.fadeButton.Name = "fadeButton";
            this.fadeButton.Size = new System.Drawing.Size( 75, 23 );
            this.fadeButton.TabIndex = 1;
            this.fadeButton.Text = "Fade";
            this.fadeButton.UseVisualStyleBackColor = true;
            this.fadeButton.Click += new System.EventHandler( this.HandleFadeButtonClick );
            // 
            // FadeForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size( 284, 142 );
            this.Controls.Add( this.fadeButton );
            this.Controls.Add( this.fadePanel );
            this.Name = "FadeForm";
            this.Text = "Fade Form";
            this.ResumeLayout( false );

        }

        #endregion

        public FadeForm()
        {
            InitializeComponent();

            this.fadeTimer = new Timer();
        }

        private void HandleFadeButtonClick( object sender, EventArgs e )
        {
            this.fadeTimer.Tick += new EventHandler( HandleFadeTimerTick );
            this.fadePanel.BackColor = Color.Red;
            this.fadeTimer.Interval = 100;
            this.fadeTimer.Start();
        }

        void HandleFadeTimerTick( object sender, EventArgs e )
        {
            Color panelColor = this.fadePanel.BackColor;

            if ( panelColor.A > 0 )
            {
                this.fadePanel.BackColor = 
                    Color.FromArgb( 
                        Math.Max( panelColor.A - 20, 0 ), 
                        panelColor.R, panelColor.G, panelColor.B );
            }
            else
            {
                this.fadeTimer.Stop();
            }
        }
    }
}

不幸的是,这种方法似乎不能用于DataGridView中的行。我不知道原因,但如果颜色的alpha分量不是255,则根本不会显示颜色。如果您能找到解决方法,这段代码可能会有所帮助。


0

简单来说,像这样的淡入淡出效果只需要一个计时器,每个滴答都将颜色逐渐恢复到正常状态。时间越快,您将从开始到结束显示更离散的颜色,并且整体效果会更加平滑(WPF可能已经内置了此功能)。

绝对不要在循环中重新绘制。正如您指出的那样,这将阻塞UI,而且您也无法控制循环所需的时间(不同的机器将以不同的时间渲染从高亮颜色到正常颜色的相同步骤)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接