如何使复选框按钮闪烁?

时间:2011-11-10 22:37:09

标签: c# .net winforms

我有一个显示为按钮的复选框。我想在检查时让它闪烁。从我发现的,我认为最简单的方法是使用计时器旋转按钮的背景颜色。

我被困住的地方是找到已选中按钮的背面颜色。当按钮被选中时,有人可以告诉我默认情况下(通过设计师)改变了背面颜色吗?没有它,我无法让计时器开始振荡。

我所拥有的是一个静音按钮。当静音处于活动状态时,我希望按钮闪烁,直到再次按下该按钮关闭静音。

如果我错了,背面颜色实际上没有变化,那么按钮会有什么变化才能让它显示出来?

代码:

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
              instructorTimer.Enabled = true;
              }
      private void instructorTimer_Tick(object sender, EventArgs e)
          {

             // interval is 2000
           if (checkBox1.BackColor == System.Drawing.SystemColors.Control)
                    checkBox1.BackColor = System.Drawing.SystemColors.ControlDark;

           else
                    checkBox1.BackColor = System.Drawing.SystemColors.Control;
           }

4 个答案:

答案 0 :(得分:1)

也许SystemColors.Control正是您所寻找的。

确保勾选了勾选事件。看起来很可疑:

private void Form1_Load(object sender, EventArgs e) {
  timer1.Tick += instructorTimer_Tick;
}

我也会立即更改颜色,以便即时反馈:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
  checkBox1.BackColor = SystemColors.ControlDark;
  timer1.Enabled = true;
}

答案 1 :(得分:1)

   private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.BackColor = Color.Green;

            Application.DoEvents();
            TimeSpan ts = new TimeSpan();

            do
            {

            }
            while (ts.Milliseconds == 2000);

            checkBox1.BackColor = SystemColors.Control;
        }

答案 2 :(得分:1)

如果您愿意使用UserControl而不是尝试重新使用Button - 以下内容应该可以正常工作,如果某些内容无效,您可以扩展它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlashyButton
{
    public partial class FlashyButton : UserControl
    {
        private CheckState _Checked = CheckState.Unchecked;

        [Browsable(true)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                lblText.Text = value;
                Invalidate();
            }
        }

        public FlashyButton()
        {
            this.CausesValidation = true;
            InitializeComponent();
            lblText.MouseClick += (sender, e) => { OnMouseClick(null); };
        }

        public void SetFont(Font WhichFont)
        {
            this.Font = WhichFont;
        }

        public CheckState GetCheckedState()
        {
            return this._Checked;
        }

        public void SetCheckedState(CheckState NewCheckState) 
        {
            this._Checked = NewCheckState;
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            this._Checked = (this._Checked == CheckState.Checked) ? CheckState.Unchecked : CheckState.Checked;
            this.BorderStyle = (this._Checked == CheckState.Checked) ? System.Windows.Forms.BorderStyle.Fixed3D : System.Windows.Forms.BorderStyle.FixedSingle;
            tmrRedraw.Enabled = (this._Checked == CheckState.Checked);
            if (this._Checked == CheckState.Unchecked)
            {
                this.BackColor = SystemColors.Control;
            }
            this.Invalidate();            //Force redraw
            base.OnMouseClick(e);
        }

        private float Percent = 100;
        private void tmrRedraw_Tick(object sender, EventArgs e)
        {
            Percent -= 2;
            if (Percent < -100) Percent = 100;
            this.BackColor = Color.FromArgb(
                255, 
                Lerp(255, SystemColors.Control.R, (int)Math.Abs(Percent)), 
                Lerp(0, SystemColors.Control.G, (int)Math.Abs(Percent)), 
                Lerp(0, SystemColors.Control.B, (int)Math.Abs(Percent))
                );
        }

        private int Lerp(int Start, int End, int Percent)
        {
            return ((int) ((float)(End - Start) * ((float)Percent / 100f)) + Start);
        }
    }
}

这里也是.Designer代码(只需替换你用这个名字制作新控件时已有的代码)

namespace FlashyButton
{
    partial class FlashyButton
    {
        /// <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 Component 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.components = new System.ComponentModel.Container();
            this.lblText = new System.Windows.Forms.Label();
            this.tmrRedraw = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // lblText
            // 
            this.lblText.AutoSize = true;
            this.lblText.Location = new System.Drawing.Point(4, 4);
            this.lblText.Name = "lblText";
            this.lblText.Size = new System.Drawing.Size(55, 17);
            this.lblText.TabIndex = 0;
            this.lblText.Text = "Sample";
            // 
            // tmrRedraw
            // 
            this.tmrRedraw.Interval = 10;
            this.tmrRedraw.Tick += new System.EventHandler(this.tmrRedraw_Tick);
            // 
            // FlashyButton
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Controls.Add(this.lblText);
            this.Name = "FlashyButton";
            this.Size = new System.Drawing.Size(148, 148);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblText;
        private System.Windows.Forms.Timer tmrRedraw;
    }
}

答案 3 :(得分:0)

当我有一个带有Appearance = Button和FlatStyle = Flat的CheckBox并希望它在选中时闪烁时,这对我有用:

private void timer_Flashing_Tick(object sender, EventArgs e)
    {
        if (checkBox_Refresh.Checked)
        {
            if (checkBox_Refresh.FlatAppearance.CheckedBackColor == Color.Red)
            {
                checkBox_Refresh.FlatAppearance.CheckedBackColor = Color.Transparent;
            }
            else
            {
                checkBox_Refresh.FlatAppearance.CheckedBackColor = Color.Red;
            }
        }
    }