您的位置:首页 > 编程语言 > Delphi

delphi RTTI的主要函数和应用示例

2012-02-09 11:44 447 查看
delphi 的RTTI为面向对象的编程提供的极大的方便,我们可以不用管对象的类型,只要通过RTTI的方法判断是否有我们需要改变的属性名和属性对象,以下是主要函数

⊙ GetTypeData 函数

⊙ GetPropInfo 函数

⊙ FindPropInfo 函数

⊙ GetPropInfos 函数

⊙ SortPropList 函数

⊙ GetPropList 函数

------------------------------------------------------

⊙ GetObjectPropClass 函数

⊙ PropType / PropIsType 函数

⊙ IsPublishedProp 函数

⊙ IsStoredProp 函数

⊙ FreeAndNilProperties 函数

⊙ SetToString / StringToSet 函数

⊙ GetEnumName / GetEnumValue / GetEnumNameValue 函数

示例1:一键设置所有控件的CustomHint属性(delphi 2010 +)

procedure TForm1.M_SetCustomHint;
var
  i:integer;
begin
  for i := 0 to self.ComponentCount - 1 do
  begin
    if IsPublishedProp(self.Components[i],'CustomHint') then
       if GetObjectProp(self.Components[i],'CustomHint')=nil then
       begin
         SetPropValue(self.Components[i],'ShowHint',true);
         SetObjectProp(self.Components[i],'CustomHint',BalloonHint1);
       end;
  end;
end;



示例2:一键设置所有控件的输入(delphi 7+)
procedure TForm1..setImeName(MyCtrl:TCustomControl);
var i:Integer;
begin 
  for i:=0 to MyCtrl.ControlCount-1 do 
  Begin 
    if IsPublishedProp(MyCtrl.Controls[i],'ImeMode') then 
      begin
          if (TWinControl(MyCtrl.Controls[i]).Enabled) and GetPropValue(MyCtrl.Controls[i],'ImeMode',false)<>imclose then  
          begin 
              SetPropValue(MyCtrl.Controls[i],'ImeMode',imopen); 
             SetPropValue(MyCtrl.Controls[i],'ImeName',Data_main.User_IME); 
         end 
      end; 
End;
end;





使用RTTI方法需要引用TypInfo单元,更多RTTI的应用,请下载
RTTI深度应用
部分演示代码




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: