2011-11-10 45 views
2

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

我卡住的地方是找到选中按钮的背景颜色。有人可以告诉我默认情况下(通过设计者)背面颜色改变为何种方式?没有这一点,我不能让计时器开始振荡。

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

如果我错了,背景颜色实际上没有改变,那么按钮的显示会发生什么变化?

代码:

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; 
      } 
+2

这是WinForms? WPF? – Jacob

+0

你使用什么技术? WinForms或WPF? – ChrisF

+0

visual Studio 2010 WinForm – eatumup

回答

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; 
} 
+0

我认为控制是正确的,但使用它我不能让它工作..也许我的代码被窃听 – eatumup

+0

@eatumup定时器有多快?您可能还需要在控件上调用刷新。此外,正如大家所提到的,尝试通过屏幕左侧的控件接受和投票更多地参与其中。他们是你的朋友。 – LarsTech

+0

哎呀,不能相信我错过了。我也在想它。间隔时间是2000年。不应该每2000毫升自动刷新一次? – eatumup

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; 
     } 
+3

你已经提出了五个问题,只接受了一个,你问的问题会占用相当一部分人的时间。请做好需要并接受一些答案。 –

+0

谢谢你指出。说实话,我从来没有真正意识到我甚至除了那个。我只是错过了所有的控制。道歉,仍在学习。 – eatumup

+0

@eatumup - 同样在这里 - 当我开始时,我甚至没有注意到每个答案选项下的“holo ticks”。当有人告诉我他们提到你接受3个或更多的答案。干杯 –

1

如果你愿意使用,而不是试图重新调整按钮用户控件 - 中以下应该很好地工作,你可以扩展它,如果有什么不像你喜欢的工作:

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

这为我工作时,我曾与外观=按钮和的FlatStyle =扁一个复选框,并希望它闪烁时检查:

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