按住不放可以在一定时间间隔内重复发送mouserDown事件的按钮。
源代码如下:
/// <summary> /// 按住不放可以在一定时间间隔内重复发送mouserDown事件的按钮。 /// </summary> public partial class ReapterButton:Button { #region Attributes private Timer timer; private MouseEventArgs meArgs; #endregion #region Properties /// <summary> /// 计时器初始延迟时间,按钮初次按下与重复事件之间间隔的毫秒数 /// </summary> [DefaultValue(400)] [Category("selfDefine")] [Description("计时器初始延迟时间,按钮初次按下与重复事件之间间隔的毫秒数")] public int InitDelay { get; set; } /// <summary> /// 重复事件之间的事件间隔,单位:毫秒 /// </summary> [DefaultValue(400)] [Category("selfDefine")] [Description("重复事件之间的事件间隔,单位:毫秒")] public int Intervals { get; set; } #endregion #region Construtor public ReapterButton() { InitializeComponent(); timer = new Timer(components); timer.Tick += new EventHandler(timer_Tick); InitDelay = 400; } #endregion #region Methods /// <summary> /// 一定事件间隔后重复触发mouserDown事件.如果事件是初次发生则在delay时间后发生 /// 以后每次迭代的时间间隔为Interva /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void timer_Tick(object sender, EventArgs e) { base.OnMouseDown(meArgs); if (timer.Enabled) { timer.Interval = Intervals; } else { timer.Interval = InitDelay; } timer.Enabled = true; } /// <summary> /// 引发计时器控件,并在Delay时间后初次引发mouserDown事件 /// </summary> /// <param name="mevent"></param> protected override void OnMouseDown(MouseEventArgs mevent) { meArgs = mevent; base.OnMouseDown(mevent); timer.Enabled = false; timer_Tick(this, EventArgs.Empty); } /// <summary> /// 使得计时器失效 /// </summary> /// <param name="mevent"></param> protected override void OnMouseUp(MouseEventArgs mevent) { base.OnMouseUp(mevent); timer.Enabled = false; } #endregion }
请大家正解。