Click here to Skip to main content
15,885,985 members
Articles / Multimedia / GDI+

TimeBarControl

Rate me:
Please Sign up or sign in to vote.
3.50/5 (5 votes)
30 May 2010CPOL1 min read 26.9K   730   26   3
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)


Written By
Software Developer Diacom Corporate & Asket Ink.
Russian Federation Russian Federation
I was born in the Murmansk city of the Russian Federation in 1984. In 2001 had experience of programming on ZX Speccy assembler. It is interesting, whether many know this platform))?
2002 - 2007: programmed on С++
2007 - 2010: .Net C#
I write programs for the medical purposes.

Comments and Discussions

 
GeneralThanks for sharing (: Pin
gutierrezgileta1-Jun-10 18:31
gutierrezgileta1-Jun-10 18:31 
GeneralShort note Pin
Petrykin Victor30-May-10 1:48
Petrykin Victor30-May-10 1:48 
GeneralRe: Short note Pin
Evgeniy Stepanow30-May-10 4:17
Evgeniy Stepanow30-May-10 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.