How can I make a control that blinks / disappears when I click it? (Windows)

When a user clicks on certain places in my control, I want to change the color of some rows and columns in my grid, and then return it to its normal color, within 500 ms or so. I have not yet decided whether to use Winforms or WPF, so recommendations for any of these technologies will work. Thank.

Edit: I understand that I can do this by simply calling Paint in a loop in the click event, setting the paint options correctly. However, I believe that this would block the user interface, and I would like to be more responsive than that.

+3
source share
3 answers

WPF . xaml, , , .

MSDN WPF, , .

+2

, :

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. , , - 255. , .

+1

, , , , . , , (WPF - ).

You definitely don't want to redraw in a loop. As you pointed out, this blocks the user interface, and you won’t be able to control how long the cycle takes (different machines will display the same number of steps from the highlight color to the normal at different time periods).

0
source

Source: https://habr.com/ru/post/1699342/


All Articles