C++ Builder 的 TForm2 上的 ProgressBar

ProgressBar on TForm2 of C++ Builder

TForm2 上,我试图制作一个从 0% 开始并需要 30 秒才能达到 100% 的 TProgressBar。一旦勾选TForm1TCheckBoxTProgressBar就会开始上涨。

我看过 Google,但这对我没有任何好处。

有什么建议吗?

TFORM1

//...

#include "Unit1.h"
#include "Unit2.h"

//---------------------------------------------------------------------------
void __fastcall TFormOne::MyCheckBoxClick(TObject *Sender)
{
  FormTwo->Show();
}

TFORM2

//...

#include "Unit2.h"
#include "Unit1.h"

//...

int MSecond = 0, MyTime = 0;

//---------------------------------------------------------------------------
__fastcall TFormTwo::TFormTwo(TComponent* Owner) : TForm(Owner)
{
  ProgressBar->Min = 0;
  ProgressBar->Max = 100;
  ProgressBar->Position = 0;
  Timer1->Enabled = true;
}

//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormCreate(TObject *Sender)
{
  MyTime = GetTickCount();
  MSecond = 0;
  Timer1->Enabled = false;
  ProgressBar->Position = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::Timer1Timer(TObject *Sender)
{
  MSecond = GetTickCount( ) - MyTime;
  if (MSecond < 30000)
    ProgressBar->Position = double Trunc(double(MSecond) / 300);
  else
  {
    ProgressBar->Position = 100;
    Timer1->Enabled = false;
  }
}

您没有正确实施 TFormTwo 您想要完成的任务。它应该看起来更像这样:

class TFormTwo : class(TFormTwo)
{
__published:
    TProgressBar *ProgressBar;
    TTimer *Timer1;
    //...
    void __fastcall FormShow(TObject *Sender);
    void __fastcall FormHide(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall Timer1Timer(TObject *Sender);
    //...
private:
    DWORD StartTime;
    //...
public:
    __fastcall TFormTwo(TComponent* Owner);
};

__fastcall TFormTwo::TFormTwo(TComponent* Owner)
    : TForm(Owner)
{
    // you should set these at design-time instead
    ProgressBar->Min = 0;
    ProgressBar->Max = 100;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormShow(TObject *Sender)
{
    ProgressBar->Position = 0;
    StartTime = GetTickCount();
    Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormHide(TObject *Sender)
{
    Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormClose(TObject *Sender, TCloseAction &Action)
{
    Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::Timer1Timer(TObject *Sender)
{
    DWORD MSecond = GetTickCount() - StartTime;
    if (MSecond < 30000)
        ProgressBar->Position = int((double(MSecond) / 30000.0) * 100.0);
    else
    {
        ProgressBar->Position = 100;
        Timer1->Enabled = false;
    }
}