全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2526
推到 Plurk!
推到 Facebook!

指定TShape元件顏色問題

答題得分者是:TWY
ycl2005
一般會員


發表:15
回覆:14
積分:5
註冊:2005-10-01

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-10-04 18:58:14 IP:124.9.xxx.xxx 訂閱
Form上有3個 TShape元件, 我想做到可以由User指定TShape元件的顏色, 目前的做法如下:

[code delphi]
for i := 0 to self.ComponentCount - 1 do begin
if self.Components[i].ClassNameIs('TShape') then begin
TShape(self.Components[i]).Brush.Color:= clWhite ;

if (TShape(self.Components[i]).Name = edit1.text ) then begin

TShape(self.Components[i]).Brush.Color:= clRed ;

end;

end;
end;
[/code]

請問有沒有更簡潔的方法, 因為實務上Form上面會有數百個元件,如果每次都要用迴圈去跑, 感覺效率不是很好.

TWY
高階會員


發表:2
回覆:133
積分:152
註冊:2009-09-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-10-05 09:28:39 IP:211.21.xxx.xxx 訂閱
方法一:依然使用迴圈。
將你的程式 self.ComponentCount 改為 Self.ControlCount 可濾掉非控制元件(如 TDataSet...TField....),可減少迴圈次數
方法二:不使用迴圈。
用一個變數記錄上次選定的 Shape Name,利用 FindComponent 方法實現不管幾百個 Shape 元件永遠只要兩步
TShape(FindComponent(strLastSelectedShapeName)).Brush.Color := clWhite;//strLastSelectedShapeName 上一次選定的 Shape 名稱
TShape(FindComponent(Edit1.Text)).Brush.Color := clRed; //這一次選定的 Shape Name


carstyc
資深會員


發表:16
回覆:254
積分:329
註冊:2003-07-18

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-10-05 10:37:23 IP:203.79.xxx.xxx 訂閱
TWY大大,方法一,self.ComponentCount 改為 Self.ControlCount ,但迴圈內還是用 self.Components[i] ,這樣陣列數目真的會對的起來嗎?
還是非控制元件,Delphi 本來預設就會把它排在後面嗎?

方法二如果用FindComponent ,它內部運作的原理,應該也是跑迴圈去尋找Component才對,似乎也不會比較快,只是寫法寫比較簡捷。


建議可以使用一個陣列先將TShape 存起來,下次就可以直接使用

[code cpp]
var
myShape:array [0..2] of TShape;
i,j:integer;
begin
j:=0;
for i := 0 to self.ComponentCount - 1 do begin
if self.Components[i].ClassNameIs('TShape') then begin
myShape[j]:=TShape(self.Components[i]);
inc(j);
end;
end;

.
.
.

for i := 0 to 2 do begin
myShape[i].Brush.Color:= clWhite ;
if (myShape[i].Name = edit1.text ) then
myShape[i].Brush.Color:= clRed ;

end;

[/code]

===================引 用 TWY 文 章===================
方法一:依然使用迴圈。
將你的程式 self.ComponentCount 改為 Self.ControlCount 可濾掉非控制元件(如 TDataSet...TField....),可減少迴圈次數
方法二:不使用迴圈。
用一個變數記錄上次選定的 Shape Name,利用 FindComponent 方法實現不管幾百個 Shape 元件永遠只要兩步
TShape(FindComponent(strLastSelectedShapeName)).Brush.Color := clWhite;//strLastSelectedShapeName 上一次選定的 Shape 名稱
TShape(FindComponent(Edit1.Text)).Brush.Color := clRed;? //這一次選定的 Shape Name



TWY
高階會員


發表:2
回覆:133
積分:152
註冊:2009-09-02

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-10-05 11:17:00 IP:211.21.xxx.xxx 訂閱
謝謝 carstyc 大大的糾正,是我疏忽了。
的確,在我認知裡直接用陣列來指向應該是最快速的方式了,沒有迴圈也不需要任何判斷。
我來把 "直接使用" 這一段 Code 給補起來...

[code delphi]
procedure TForm1.Button2Click(Sender: TObject);
var
myShape:array [0..2] of TShape;
i,j : Integer;
sLastSelected,sCurrentSelected : String;
iLastSelectedIndex,iCurrentSelectedIndex : Integer;

begin
//先跑 Loop 將 Shape1~3 Assign 到 Array 中,這個 Loop 是必要的 (只要執行一次即可)
j:=0;
for i := 0 to self.ComponentCount - 1 do begin
if self.Components[i].ClassNameIs('TShape') then begin
myShape[j]:=TShape(self.Components[i]);
inc(j);
end;
end;

{
for i := 0 to 2 do begin
myShape[i].Brush.Color:= clWhite ;
if (myShape[i].Name = edit1.text ) then
myShape[i].Brush.Color:= clRed ;
end;
}

//假設上次選擇的是 Shape2,這次選擇的是 Shape3,元件命名規則遵守 Shape1,Shape2... 的話
//利用 Index 特性捨取 Loop 方式"直接使用"
sLastSelected := 'Shape2';
sCurrentSelected := 'Shape3';
iLastSelectedIndex := StrToInt(Copy(sLastSelected,6,Length(sLastSelected)-5)) - 1;
iCurrentSelectedIndex := StrToInt(Copy(sCurrentSelected,6,Length(sCurrentSelected)-5)) - 1;
(myShape[iLastSelectedIndex] as TShape).Brush.Color := clWhite;
(myShape[iCurrentSelectedIndex] as TShape).Brush.Color := clRed;

end;

[/code]






===================引 用 carstyc 文 章===================
建議可以使用一個陣列先將TShape 存起來,下次就可以直接使用

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