RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
C++

How to put a progress bar on a Status Bar …

Author: Embarcadero USA

 Question and Answer Database

FAQ2293C.txt   How to put a progress bar on a Status Bar ...
Category   :VCL
Platform    :All
Product    :C++Builder  3.x

Question:
How Do I put a Proress bar on a status bar.

Answer:
Here is an example:

This is the form:

object Form1: TForm1
  Left = 191
  Top = 108
  Width = 226
  Height = 140
  Caption = 'Form1'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object StatusBar1: TStatusBar
    Left = 0
    Top = 94
    Width = 218
    Height = 19
    Panels = 
    SimplePanel = False
    OnResize = StatusBar1Resize
  end
  object Button1: TButton
    Left = 18
    Top = 30
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end
 
//----------------------------------------------------------
This is unit1.cpp

//---------------------------------------------------------------------------
#include 
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    ProgressBar = new TProgressBar ( StatusBar1 );
    ProgressBar->Parent = StatusBar1;

    ProgressBar->Position = 0;
    ProgressBar->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StatusBar1Resize(TObject *Sender)
{
    int Size = StatusBar1->Width;
    for ( int i = 1; i Panels->Count; i++ )
      Size -= StatusBar1->Panels->Items[i]->Width;

    //resize the first panel based on the form width
    StatusBar1->Panels->Items[ 0 ]->Width = Size;
    RECT Rect;
    StatusBar1->Perform ( SB_GETRECT,  0,  (LPARAM)&Rect );

    ProgressBar->Top = Rect.top;
    ProgressBar->Left = Rect.left;
    ProgressBar->Width = StatusBar1->Panels->Items [ 0 ]->Width;
    ProgressBar->Height = Rect.bottom - Rect.top;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ProgressBar->Show();
  ProgressBar->Position = 50;  
}
//---------------------------------------------------------------------------
 

Here is Unit1.h

//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TStatusBar *StatusBar1;
  TButton *Button1;
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall StatusBar1Resize(TObject *Sender);
  void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
  TProgressBar *ProgressBar;
public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
 


7/2/98 10:32:32 AM
 

Article originally contributed by Alan Ellis


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Learn More About Upgrading

Related posts
C++DelphiTech Partner

Easily Download And Install A Robust Syntax Highlighter For Delphi And C++ Builder From GetIt

C++Delphi

Powerful Set Of 200+ VCL Components By DevExpress For Delphi Business Solutions

C++DelphiRAD Studio

Automate Excel and Word Directly From Delphi Using Office Component Suite

C++DelphiTech Partner

Set of utilities for Delphi and C++Builder to speed up your development process

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES