日期:2014-05-19  浏览次数:20897 次

如何开发一个背景颜色可以闪烁的Button的控件
如何开发一个Button控件使背景颜色可以闪烁?

------解决方案--------------------
处理它的Paint事件,添加一个Timer定时指定要绘制的颜色让Button绘制
------解决方案--------------------
你这样来做:
class ButtonEx:Button
{
private Timer timer = new Timer();
public ButtonEx()
{
timer.Enabled = true;
timer.Interval = 500;
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{
if (this.BackColor == Color.Transparent)
{
this.BackColor = Color.Green;
}
else
{
this.BackColor = Color.Transparent;
}
}
protected override void Dispose(bool disposing)
{
this.timer.Enabled = false;
this.timer.Dispose();
base.Dispose(disposing);
}
}

------解决方案--------------------
在void timer_Tick(object sender, EventArgs e)
里面不直接更新背景,改成用delegate 和 event来更新
尝试一下怎样
------解决方案--------------------
问题1、控件未加载完毕;
2、应该在线程UI里修改,
下面没有在UI里修改,直接用时钟修改,但可以用
using System;
using System.Windows.Forms;
using System.Drawing;
namespace LessonManager
{
/// <summary>
/// Class3 的摘要说明。
/// </summary>
class ButtonEx:Button
{
private Timer timer = new Timer();
public delegate void ChangColor(Color c);
public ButtonEx()
{
timer.Enabled = true;
timer.Interval = 500;
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{

if (this.BackColor == Color.Transparent)
{
this.BackColor=Color.Green;
}
else
{
this.BackColor=(Color.Transparent);
}
}

private void Changing(Color c)
{
this.BackColor=c;
}
protected override void Dispose(bool disposing)
{
this.timer.Enabled = false;
this.timer.Dispose();
base.Dispose(disposing);
}
}
}