C++ Builder

La Isla Bonita



Solid Progress Bar
Solid Progress Bar


Description

This example shows how to create a solid progress bar to display the played time of a media. The progress bar is created dynamically by creating an object of the TSolidProgressBar class. All components on Form1 are created dynamically.

SolidProgressBar.cpp

#include <vcl\vcl.h>
#pragma hdrstop

#include "SolidProgressBar.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
TTimer* Timer1;
TButton* Button1;
TMediaPlayer* MediaPlayer1;
TPanel* Panel1;
TOpenDialog* OpenDialog1;


//Create Global Object
TSolidProgressBar oSolidProgressBar;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
    Width  = 800;
    Height = 600;
    Color  = clPurple;
    Position = poScreenCenter;

	//Create a Timer
    Timer1 = new TTimer( this );
    Timer1->Enabled  = false;
    Timer1->Name     = "Timer1";
    Timer1->Interval = 100;
    Timer1->OnTimer  = Timer1Timer;

	//Create the Playback Panel
    Panel1 = new TPanel( this );
    Panel1->Parent = this;
    Panel1->Enabled = true;
    Panel1->Visible = true;
    Panel1->Top     = 100;
    Panel1->Height  = 250;
    Panel1->Left    = 25;
    Panel1->Width   = 250;
    Panel1->Color   = clRed;
    Panel1->Caption = "Playback Panel";
    
	//Create the MediaPlayer
    MediaPlayer1 = new TMediaPlayer( this );
    MediaPlayer1->Parent = this;
    MediaPlayer1->Name = "MediaPlayer1";
    MediaPlayer1->Enabled = true;
    MediaPlayer1->Visible = true;
    MediaPlayer1->Top     =  25;
    MediaPlayer1->Height  =  25;
    MediaPlayer1->Left    =  Panel1->Left;
    MediaPlayer1->Width   = 300;
    MediaPlayer1->Display = Panel1;
    MediaPlayer1->DeviceType = dtAutoSelect;
    MediaPlayer1->OnClick    = MediaPlayer1Click;

	//Create the button to open the media
    Button1 = new TButton( this );
    Button1->Parent  = this;
    Button1->Enabled = true;
    Button1->Visible = true;
    Button1->Top     =  25;
    Button1->Height  =  MediaPlayer1->Height;
    Button1->Left    =  MediaPlayer1->Left + MediaPlayer1->Width + 55;
    Button1->Width   = 100;
    Button1->Caption = "Open Media";
    Button1->OnClick = Button1Click;

	//Create the Open Dialog to open a media
    OpenDialog1 = new TOpenDialog( this );
    OpenDialog1->Name = "OpenDialog1";

	//Create a solid progress bar
    oSolidProgressBar.New( 25, PROGRESSBAR_WIDTH, 60, 25,
                           clRed, this  );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
  if( OpenDialog1->Execute() )
  {
      Update();
      Screen->Cursor = crHourGlass;
      MediaPlayer1->FileName=OpenDialog1->FileName;

      try
      {
          MediaPlayer1->Open();
          oSolidProgressBar.SetPosition( 0 );          
      }
      catch (...)
      {
          Application->MessageBox( "Open File Error",
                                    mtWarning, MB_ICONWARNING | MB_OK );
      }
      Screen->Cursor = crArrow;

      MediaPlayer1->DisplayRect = Rect( 0, 0, 0, 0);

      Panel1->Top       = 100;
      Panel1->Width     = MediaPlayer1->DisplayRect.Right;
      Panel1->Height    = MediaPlayer1->DisplayRect.Bottom;
      Panel1->Visible = true;
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
//
    //Set the Position of the Solid Prograss Bar according
    //to the position of the MediaPlayer component.
    oSolidProgressBar.SetPosition(
         (PROGRESSBAR_WIDTH * MediaPlayer1->Position / MediaPlayer1->Length) );

    //Disable the Timer if playback has finished
    if( oSolidProgressBar.GetPosition() >= PROGRESSBAR_WIDTH )
    {
        Timer1->Enabled = false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MediaPlayer1Click(TObject *Sender,
      TMPBtnType Button, bool &DoDefault)
{
//
    if( Button == btPlay )
    {
        Timer1->Enabled = true;
    }
    else
    {
        if ( Button == btStop )
        {
            Timer1->Enabled = false;
        }
    }

    DoDefault = true;
}

SolidProgressBar.h

#ifndef SolidProgressBarH
#define SolidProgressBarH
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\ExtCtrls.hpp>
#include <vcl\MPlayer.hpp>
#include <vcl\Dialogs.hpp>
//-------------------------------------------------------
const int PROGRESSBAR_WIDTH = 450;
//-------------------------------------------------------

//TSolidProgressBar Classs
class TSolidProgressBar
{
  private:
    TPanel* Panel1;
    TPanel* ProgressBar;

  public:

    //New(): Create Solid Progress Bar
    void New( int left, int width, int top, int height,
              TColor color, TWinControl* p )
    {
        Panel1 = new TPanel( p );
        Panel1->Parent = p;
        Panel1->Top    = top;
        Panel1->Left   = left;
        Panel1->Width  = width + 4;
        Panel1->Height = height;
        //-------------------------------
        Panel1->BevelInner  = bvLowered;
        Panel1->BevelOuter  = bvLowered;
        //-------------------------------
        Panel1->Color = clBlack;

        ProgressBar = new TPanel( p );
        ProgressBar->Parent = p;
        ProgressBar->Top    = Panel1->Top + 2;
        ProgressBar->Width  = 0;
        ProgressBar->Left   = Panel1->Left + 2;
        ProgressBar->Height = Panel1->Height - 4;
        //-------------------------------
        ProgressBar->BevelInner  = bvNone;
        ProgressBar->BevelOuter  = bvNone;
        //-------------------------------
        ProgressBar->Color = color;
    }

    void SetPosition( int width )
    {
        ProgressBar->Width = width;
    }
    
    int GetPosition()
    {
        return ProgressBar->Width;
    }     
};

class TForm1 : public TForm
{
__published:	// IDE-managed Components
	void __fastcall Button1Click(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall MediaPlayer1Click(TObject *Sender,
          TMPBtnType Button, bool &DoDefault);
private:	// User declarations
public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------
extern TForm1 *Form1;

#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.