2015-08-15 81 views
0

TForm2上,我試圖使TProgressBar開始於0%,並需要30秒才能達到100%。只要TForm1TCheckBox被選中,TProgressBar就會開始上升。C++ Builder的TForm2上的ProgressBar

我在谷歌上看過,但是這樣的事情讓我沒有什麼好處。

有什麼建議嗎?

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; 
    } 
} 
+0

而你的問題是......? –

+0

順便說一下,**不要**在C++中使用'OnCreate'事件。這是一個Delphi成語,它會在C++中創建非法行爲。改用構造函數。事實上,你現在在'TFormTwo :: FormCreate()'中的所有代碼都不屬於這個項目開始,所以把它除掉。 –

回答

1

你沒有實現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; 
    } 
} 
+0

非常感謝Remy Lebeau。它工作得很好。哎唷,我忘了問一件事。如果我想在'30秒'/'100%的ProgressBar'後自動關閉我的電腦,那麼放置'system(「shutdown -s -f -t 0」);'?我試着把它放在'else {'語句之後,但是我很抱歉,它根本不適用於我。 –

+0

不要使用'system()'。相反,使用['ExitWindowsEx()'](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868.aspx),['InitiateShutdown()'](https:// msdn。 microsoft.com/en-us/library/windows/desktop/aa376872.aspx),['InitiateSystemShutdown()'](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376873.aspx )或['InitiateSystemShutdownEx()'](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376874.aspx)。至於*哪裏*稱它,你有正確的位置。您只需確保您的應用具有足夠的*權限即可關閉計算機。 –