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

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深度应用  部分演示代码





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值