線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1560
推到 Plurk!
推到 Facebook!

如何在線程中使用動態宣告的元件....

尚未結案
hipig
高階會員


發表:31
回覆:75
積分:111
註冊:2007-01-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-03-09 19:37:51 IP:140.126.xxx.xxx 未訂閱
我在Form1中動態宣告了一個Shape元件
FormCreate中new出來
程式很正確的顯示
玵我現在希望可以由Thread來控制這new出來元件的顏色(用於繪圖)
請問該用何種方式才能控制
謝謝
daldal
高階會員


發表:6
回覆:102
積分:226
註冊:2007-06-18

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-03-11 20:14:39 IP:220.130.xxx.xxx 訂閱
其實你應該先把你的問題簡單化

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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-03-12 23:57:54 IP:123.193.xxx.xxx 未訂閱
閒閒沒事,寫寫程式
程式碼謹供參考.....

[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]

[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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-03-13 10:20:02 IP:140.126.xxx.xxx 未訂閱
感謝兩位回覆
在兩位回覆前我試了一些方式
想問問看下面這樣作法是否不太恰當

觀念上我認為不同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]

這樣作是否可行呢(我實際作是沒有問題)
或有何不妥
還請指教
系統時間:2024-04-25 13:00:04
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!