關於計數器,時間計算問題!! |
答題得分者是:jow
|
n890377
一般會員 發表:17 回覆:16 積分:21 註冊:2006-07-13 發送簡訊給我 |
各位工程師好 我有個計數器應用的問題 希望有人能幫忙解答一下,
我利用一個圓 0至360度 分成8個等份 ex:90度~135度(其中一等份), 在程式中進行影像處理 當我的指標落在 ex:90度~135度時, 利用計數器 重0秒開始 至3秒時,則 Edit1->Text = "1"; 判斷此範圍為 "1" 若在3秒還沒到 指標移動至其他角度範圍時 則Edit1->Text = "0"; 並且計數器重新由0開始算起 只有在指標落在 某個角度範圍內滿3秒 才顯示出範圍1~8(8等份) 若不足3秒 就離開此範圍再重新由0秒開始算起 我現在卡在不知如何離開某角度範圍後 時間再重0秒開始算起 因為時間會一直算下去 希望有高手能幫忙解答 非常謝謝!!
------
. |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
解題測試程式碼下載:
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=90848 [code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />#include //--------------------------------------------------------------------------- class TForm1;//pre-declaration //--------------------------------------------------------------------------- class TCountDownThread : public TThread { protected: TForm1 *FOwner; bool IsTimeout; void __fastcall Execute(void); void __fastcall UpdateVCL(void); public: __fastcall TCountDownThread(TForm1* Owner); }; //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TTimer *Timer1; void __fastcall FormCreate(TObject *Sender); void __fastcall Timer1Timer(TObject *Sender); void __fastcall FormClose(TObject *Sender, TCloseAction &Action); private: int FAngle; int FArea; unsigned int FCountDown[8]; void __fastcall SetAngle(int Value); void __fastcall SetArea(int Value); int __fastcall GetCountDown(int Index); protected: TCountDownThread *CountDownThread; public: TLabel* Labels[8]; __fastcall TForm1(TComponent* Owner); __property int Angle = {read=FAngle, write=SetAngle}; __property int Area = {read=FArea, write=SetArea}; __property int CountDown[int Index] = {read=GetCountDown}; }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- int TIME_OUT = 3000; //3秒 //--------------------------------------------------------------------------- __fastcall TCountDownThread::TCountDownThread(TForm1* Owner): TThread(true) { FOwner = Owner; FreeOnTerminate = false; Resume(); } //--------------------------------------------------------------------------- void __fastcall TCountDownThread::Execute(void) { while(!Terminated){ int i = FOwner->Area; unsigned int c = FOwner->CountDown[i]; IsTimeout = (c>0) && (GetTickCount()>c); Synchronize(UpdateVCL); Application->ProcessMessages(); Sleep(10); } } //--------------------------------------------------------------------------- void __fastcall TCountDownThread::UpdateVCL(void) { TColor color; AnsiString S; unsigned diff; for(int i=0; i<8; i ){ if(i==FOwner->Area){ diff = GetTickCount()-FOwner->CountDown[i]; S = IntToStr(diff); if(IsTimeout) color = clRed; else color = clBlue; }else{ S = ""; color = clGray; } FOwner->Labels[i]->Caption = S; FOwner->Labels[i]->Color = color; FOwner->Labels[i]->Update(); } } //--------------------------------------------------------------------------- // // // //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { Randomize(); memset(&FCountDown,0,sizeof(FCountDown)); FAngle = 0; FArea = 0; FCountDown[0] = GetTickCount() TIME_OUT; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { int W = 50; int H = 30; TRect R = Rect(10,20,10 W,20 H); for(int i=0; i<8; i ){ Labels[i] = new TLabel(this); Labels[i]->Parent = this; Labels[i]->AutoSize = false; Labels[i]->Transparent = false; Labels[i]->Alignment = taCenter; Labels[i]->Layout = tlCenter; Labels[i]->Color = clBlack; Labels[i]->Font->Color = clWhite; Labels[i]->BoundsRect = R; OffsetRect(R, W 1, 0); } CountDownThread = new TCountDownThread(this); Timer1->Interval = TIME_OUT; Timer1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { if(CountDownThread){ Timer1->Enabled = false; CountDownThread->Terminate(); CountDownThread->WaitFor(); delete CountDownThread; } } //--------------------------------------------------------------------------- int __fastcall TForm1::GetCountDown(int Index) { if(Index<0||Index>7) return 0; else return FCountDown[Index]; } //--------------------------------------------------------------------------- void __fastcall TForm1::SetAngle(int Value) { Value = Value % 360; if(Value!=FAngle){ FAngle = Value; Area = FAngle / 45; } } //--------------------------------------------------------------------------- void __fastcall TForm1::SetArea(int Value) { if(Value!=FArea){ FCountDown[FArea] = 0; //Reset FCountDown[Value] = GetTickCount() TIME_OUT; //設定倒數秒數 FArea = Value; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { this->Angle = random(MaxInt) % 360; //改變 Timer1 的 Interval Timer1->Interval = random(TIME_OUT) TIME_OUT/2; } //--------------------------------------------------------------------------- [/code]
編輯記錄
jow 重新編輯於 2007-10-16 19:46:22, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |