15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Как поместить Progress Bar на Status Bar.

Компилятор: C++ Builder 3.x

Для формы:

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 = <
      item
        Width = 50
      end>
    SimplePanel = False
    OnResize = StatusBar1Resize
  end
  object Button1: TButton
    Left = 18
    Top = 30
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end
 
//----------------------------------------------------------

Это unit1.cpp

//--------------------------------------------------------------------
#include <vcl.h>
#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 < StatusBar1->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;  
}
//--------------------------------------------------------------------

Это Unit1.h

//--------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//--------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//--------------------------------------------------------------------
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