首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProgressBar on TForm2 of C++ Builder

ProgressBar on TForm2 of C++ Builder
EN

Stack Overflow用户
提问于 2015-08-15 19:28:05
回答 1查看 2.4K关注 0票数 0

TForm2上,我试图制作一个从0%开始,到100%需要30秒的TProgressBarTProgressBar一检查TCheckBox of TForm1就会开始上升。

我看过谷歌,但这并没有给我带来什么好处。

有什么建议吗?

TFORM1

代码语言:javascript
复制
//...

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

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

TFORM2

代码语言:javascript
复制
//...

#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;
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-15 23:19:08

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

代码语言:javascript
复制
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);
};

代码语言:javascript
复制
__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;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32028504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档