Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / GDI+

TimeBarControl

3.50/5 (5 votes)
30 May 2010CPOL1 min read 27.2K   734  
Control to display the time progress line
TimeBar.jpg

Introduction

In advance, I shall make a reservation that my English is very bad. Sorry!

Control capabilities:

  • Dragging the cursor on a scale
  • Two modes of nice background
  • Custom colors for mixed background

Background

Events:

  • Tick() - Occurs every time when event Tick() occurs by Internal Timer object
  • TimeHasExpired - Occurs when time has expired
  • UserBeginChangeTime - When user captures the cursor on TimeBar
  • UserEndChangeTime - Occurs when MouseUp occurs

While cursor is captured by the mouse - for each movement of the mouse, there is event Tick() and internal time is changed. In property 'Now' is the current time.

Using the Code

For using this TimeBar, just add it to your project, change settings as you like and setup event handlers.

When method TimeBar.Start() is caused, method Timer.Start() through the set interval causes event handler Timer_Tick (object sender, EventArgs e). In it, the handler causes the Advance() method.

C#
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);
}        

At this time, backgroundWorker1 runs background to roll:

C#
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();
}

I think that my control requires editing and optimization. I will come back to this work later. I await your comments!

History

  • 19.05.2010 - version 1.0.0.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)