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

RTTI 問題

尚未結案
BGman
初階會員


發表:28
回覆:85
積分:42
註冊:2003-01-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-08 13:49:16 IP:211.22.xxx.xxx 未訂閱
在BCB 深度歷險一書中,第二章有談到 RTTI 以下的程式碼,是掃描Form上所有元件,將Color 的屬性都設成紅色
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  PPropInfo PropInfo;
  for (int i = 0; i < ComponentCount; i  )
  {
    PropInfo = GetPropInfo((TTypeInfo *)Components[i]->ClassInfo(),"Color");
    if ( PropInfo )
      SetOrdProp( Components[i], PropInfo, clRed);
  }
}
可是書上最後有提供一個技巧,應該要判斷屬性型態是否為 TColor 以防止萬一有一個元件Color 的屬性,又剛好型態是 String 我利用 Code Insight 的 Code completion 功能,找到 GetPropInfo(TTypeInfo *TypeInfo,AnsiString PropName,TTypeKind aKinds); 於是我便寫了下面的程式碼,可是卻出現錯誤! 不接受三個參數。請大家幫我指點迷津! PropInfo = GetPropInfo((TTypeInfo *)Components[i]->ClassInfo() ,"Color", (Typinfo::TTypeKind)tkInteger); 我在想是不是還有其他函式可以用,可是我一搜尋,都會跑到TPropertyEditor 而且看到的說明都和我所需要的有偏差,舉例來說... Help 中查到的是 Typinfo::PPropInfo __fastcall GetPropInfo(void); 可是實際上用到的是 *PPropInfo GetPropInfo(TTypeInfo *, AnsiString *); 或是 *PPropInfo GetPropInfo(TTypeInfo *, AnsiString *, TTypeKind *);
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-09 11:21:30 IP:61.231.xxx.xxx 未訂閱
BCB 的說明文件不足是 BCB 使用者心中的痛... 找了一下 include 目錄下所有 GetPropInfo 發現主要在 TypInfo.hpp 中有下述 overload 的宣告
extern PACKAGE PPropInfo __fastcall GetPropInfo(System::TObject* Instance, const AnsiString PropName, TTypeKinds AKinds)/* overload */;
extern PACKAGE PPropInfo __fastcall GetPropInfo(TMetaClass* AClass, const AnsiString PropName, TTypeKinds AKinds)/* overload */;
extern PACKAGE PPropInfo __fastcall GetPropInfo(PTypeInfo TypeInfo, const AnsiString PropName)/* overload */;
extern PACKAGE PPropInfo __fastcall GetPropInfo(PTypeInfo TypeInfo, const AnsiString PropName, TTypeKinds AKinds)/* overload */;
一開始我也以為您的程式碼是沒錯的,初看了錯誤訊息還看不出來,於是試著強迫轉型(like you do),但仔細看錯誤訊息後才發現,這不是強迫轉型可以作的。 TTypeKinds 是一個 TTypeKind 的 Set 可以放很多個 TTypeKind,而 tkInteger 只是一個元素,不是一個 Set ,所以 BCB 無法幫您轉換。 改用以下的程式即可順利判斷屬性是何種參數。
  PPropInfo PropInfo;
  TTypeKinds typekind;
  typekind= typekind<ClassInfo(),"Color",typekind);
    if ( PropInfo )
      SetOrdProp( Components[i], PropInfo, clRed);
  }
沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-09 11:26:02 IP:61.231.xxx.xxx 未訂閱
再貼一次 code 好像 << 顯示會有問題
  PPropInfo PropInfo;
  TTypeKinds typekind;
  typekind= typekind<<tkInteger;
  for (int i = 0; i < ComponentCount; i  )
  {
    PropInfo = GetPropInfo((TTypeInfo *)Components[i]->ClassInfo(),"Color",typekind);
    if ( PropInfo )
      SetOrdProp( Components[i], PropInfo, clRed);
  }
沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com
BGman
初階會員


發表:28
回覆:85
積分:42
註冊:2003-01-10

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-09 21:41:56 IP:211.76.xxx.xxx 未訂閱
原來如此,dllee 桑你實在太厲害了! 小弟感激不盡! 另外,下面有一題,我一直解不出來,能夠麻煩版你抽空幫我看一下嗎? http://delphi.ktop.com.tw/topic.php?TOPIC_ID=25477
YuHeng
一般會員


發表:8
回覆:13
積分:4
註冊:2003-02-26

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-05-09 11:04:52 IP:61.56.xxx.xxx 未訂閱
引言: 再貼一次 code 好像 << 顯示會有問題
  PPropInfo PropInfo;
  TTypeKinds typekind;
  typekind= typekind<ClassInfo(),"Color",typekind);
    if ( PropInfo )
      SetOrdProp( Components[i], PropInfo, clRed);
  }
沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
請教一下... 我要設定bool的資料,找不到設bool的指令,目前是用SetOrdProp,如: for (int i = 0; i < ComponentCount; i ) { PropInfo = GetPropInfo((TTypeInfo *)Components[i]->ClassInfo(),"Enabled",typekind); if ( PropInfo ) SetOrdProp( Components[i], PropInfo, 1); } 想請問一下,有沒有設bool的專用指令,找help都找不到...... 謝謝先!!
系統時間:2024-05-02 22:22:31
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!