2011-11-10 150 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; 
      } 
     } 
    }