如何在線程中使用動態宣告的元件.... |
尚未結案
|
hipig
高階會員 發表:31 回覆:75 積分:111 註冊:2007-01-15 發送簡訊給我 |
|
daldal
高階會員 發表:6 回覆:102 積分:226 註冊:2007-06-18 發送簡訊給我 |
其實你應該先把你的問題簡單化
1.在單線程下,操作動態的元件 2.做多線程的考量 然後你會發現,其實兩個都不難, 難是難在你一次想要解決兩個問題 1.的solution 預先寫好元件的事件,然後在new的時候,把事件指定上去 例如你想要寫拖拉Shape,那就預先寫以下事件: On Mouse Move, On Mouse Up, On Mouse Down, 偵測滑鼠是否有通過Shape, 壓下右鍵開始拖拉等等 2.的solution 基本上,以你舉的例子用多線程真的是怪怪的@@ 最好是就你第一部分的結果,把真的有需要的地方,再實現多線程的運作 |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
閒閒沒事,寫寫程式
程式碼謹供參考..... [code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />#include //--------------------------------------------------------------------------- class TColorThread : public TThread { protected: TShape *FShape; void __fastcall Execute(); void __fastcall UpdateColor(); public: __fastcall TColorThread(TShape *O); }; //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: public: __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TColorThread::TColorThread(TShape *O) : TThread(true) { FreeOnTerminate = true; FShape = O; Resume(); } //--------------------------------------------------------------------------- void __fastcall TColorThread::Execute() { do{ Synchronize(UpdateColor); Application->ProcessMessages(); Sleep(random(200) 200); }while(!Terminated); } //--------------------------------------------------------------------------- void __fastcall TColorThread::UpdateColor() { if(FShape) FShape->Brush->Color = TColor(RGB( random(0x100), random(0x100), random(0x100))); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { Randomize; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int w = (ClientWidth-120)/3; int h = ClientHeight/3; for(int i=0; i<9; i ){ TShape *O = new TShape(this); O->Shape = TShapeType(random(MaxInt)%6); TRect R = Rect(0,0,w,h); OffsetRect(&R,w*(i%3),h*(i/3)); InflateRect(&R,-5,-5); O->BoundsRect = R; O->Parent = this; new TColorThread(O); } Button1->Enabled = false; } //--------------------------------------------------------------------------- [/code] |
hipig
高階會員 發表:31 回覆:75 積分:111 註冊:2007-01-15 發送簡訊給我 |
感謝兩位回覆
在兩位回覆前我試了一些方式 想問問看下面這樣作法是否不太恰當 觀念上我認為不同Form中傳遞變數要採用extern宣告變數 現在我在一個控制變數的檔案中宣告 mavariable.h [code cpp] #ifndef mavariableH #define mavariableH //--------------------------------------------------------------------------- #include extern TShape *Shape1, *Shape2, *Shape3; //--------------------------------------------------------------------------- #endif [/code] mavariable.cpp [code cpp] #include #include "mavariable.h" #pragma hdrstop //--------------------------------------------------------------------------- TShape *Shape1, *Shape2, *Shape3; //--------------------------------------------------------------------------- #pragma package(smart_init) [/code] 而後在主Form叫出來用 [code cpp] #include "mavariable.h" #include "Thread.h" void __fastcall TForm1::FormCreate(TObject *Sender) { Shape1 = new TShape(this); Shape2 = new TShape(this); Shape3 = new TShape(this); StatusBar1->Panels->Items[0]->Style=psOwnerDraw; Shape1->Shape=stCircle; Shape1->Pen->Color=clBlack; Shape1->Height=15; Shape1->Width=15; Shape1->Parent = StatusBar1; Shape1->Left=StatusBar1->Left 3; Shape1->Top= 3; (下略) Thread->Resume(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { delete Shape1, Shape2, Shape3; } [/code] 線程中直接用以下方式控制 [code cpp] void __fastcall ThreadRS::Execute() { while(!Terminated) { Synchronize(Button); } FreeOnTerminate = true; } //--------------------------------------------------------------------------- void __fastcall ThreadRS::Button(void) { if(......) Shape1->Brush->Color=clLime; else Shape1->Brush->Color=clGreen; (下略) } [/code] 這樣作是否可行呢(我實際作是沒有問題) 或有何不妥 還請指教 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |