65.9K
CodeProject 正在变化。 阅读更多。
Home

TimeBarControl

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (5投票s)

2010 年 5 月 19 日

CPOL
viewsIcon

27727

downloadIcon

765

显示时间进度线的控件。

TimeBar.jpg

引言

首先声明,我的英语很差。抱歉!

控件功能

  • 在刻度上拖动光标
  • 两种漂亮的背景模式
  • 自定义混合背景颜色

背景

事件

  • Tick() - 每当内部计时器对象发生 Tick() 事件时触发
  • TimeHasExpired - 当时间已过期时触发
  • UserBeginChangeTime - 当用户在 TimeBar 上捕获光标时
  • UserEndChangeTime - 当发生 MouseUp 时触发

当光标被鼠标捕获时 - 每次鼠标移动,都会触发 Tick() 事件,并且内部时间会改变。在属性 'Now' 中是当前时间。

Using the Code

要使用此 TimeBar,只需将其添加到您的项目中,根据需要更改设置并设置事件处理程序即可。

当调用方法 TimeBar.Start() 时,方法 Timer.Start() 通过设置的间隔触发事件处理程序 Timer_Tick (object sender, EventArgs e)。 在其中,处理程序会触发 Advance() 方法。

public void Start()
{
    timer.Start();
    if(!backgroundWorker.IsBusy)
    {
        backgroundWorker.RunWorkerAsync();
        now = 0;
        cursor_location.X = line_left;
        if(Tick != null)
            Tick(this, null);// this is for subscribers
    }
}

private void timer_Tick(object sender, EventArgs e)
{
    this.Advance();
}

/// <summary>
/// To advance the cursor on value of an interval
/// </summary>
public void Advance()
{
    //move cursor
    cursor_location.X += pixToSec / (1000/timer.Interval);
    //change time
    now += (float)1 / (1000/timer.Interval);
    if (now >= duration)            
    {
        Stop();
    }
    //notify subscribers
    else if (Tick != null) Tick(this, null);
}        

此时,backgroundWorker1 在后台运行以滚动

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        while (!worker.CancellationPending)
        {
            Application.DoEvents();
            System.Threading.Thread.Sleep((int)lightSpeed);
            worker.ReportProgress(0);
        }
    }
    
    float pix = 0;
    private void backgroundWorker_ProgressChanged
	(object sender, System.ComponentModel.ProgressChangedEventArgs e)
    {
        //this event treats in Form thread. 
        //And changes in this handler is thread-self.
       
        //for LightStyle.Light            
        focus += 0.01f;
        if (focus > 1.0f)
        {
            focus = 0f;
            //Delay between cycles
            System.Threading.Thread.Sleep(100);
        }
        
        //for LightStyle.Fill
        pix++;            
        if (pix % this.Width == 0)
            pix = 0;
            
        //Parabola function for increasing to center
        scale = (focus - 0.5f) * (focus - 0.5f);
        scale *= 4;
        scale = 1 - scale;
        
        this.BackgroundImage.Dispose();
        this.BackgroundImage = MakeBackground(focus, scale);
        
        this.Refresh();
    }

我认为我的控件需要编辑和优化。我稍后会回到这项工作。期待您的评论!

历史

  • 2010年5月19日 - 版本 1.0.0.0
© . All rights reserved.