Board index » cppbuilder » Changing TProgressBar color...best solution...?

Changing TProgressBar color...best solution...?


2004-06-04 06:57:22 AM
cppbuilder76
hi dear builders,
I'm chanmging the color of TProgrssBar into red directly in the form
constructor..
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
SendMessage(ProgressBar->Handle, PBM_SETBARCOLOR, 0, 0x000606AA);
}
but sometimes it turns into "windows default" blue again...why ???
where (in which event) should i place the code the keep the red color all
the time...?
Oren
 
 

Re:Changing TProgressBar color...best solution...?

"Oren Halvani" < XXXX@XXXXX.COM >wrote:
Quote
[...] but sometimes it turns into "windows default" blue
again...why ???
No clue.
Quote
where (in which event) should i place the code the keep the
red color all the time...?
On my list of things to do is to create a solid progress bar
of our own. While surfing the web, I collected some code that
will be my starting point and it shouldn't take too much effort
for you to convert the code for yourself:
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
~ JD
 

Re:Changing TProgressBar color...best solution...?

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
I'm chanmging the color of TProgrssBar into red directly
in the form constructor..
That may be too soon. Try delaying it until the form is fuly constructed
and visible first.
Quote
but sometimes it turns into "windows default" blue again...why ???
Probably because the progress bar window was destroyed and recreated as a
result of some property value change somewhere. Thus any custom settings
you applied were lost.
Quote
where (in which event) should i place the code the
keep the red color all the time...?
I would not recommend that approach to begin with. You would be better
off.finding a third-party progress bar that already supports what you need.
Or else write your own component, and simply apply your custom coloring in
the overriden CreateWnd() method.
Gambit
 

{smallsort}