如何获取一个控件的所有事件及属性,并把所有的相关事件列出来(例:Tbutton.onClick列表下可能有Button1Click,Button2Click,Button3Click等).如DELPHI中的属性编辑器

解决方案 »

  1.   


    procedure   GetClassProperties(AClass:   TObject;   AStrings:   TStrings);   
      var   
          PropList:   PPropList;   
          ClassTypeInfo:   PTypeInfo;   
          ClassTypeData:   PTypeData;   
          I:   integer;   
          NumProps:   Integer;   
      begin   
          ClassTypeInfo   :=   AClass.ClassInfo;   
          ClassTypeData   :=   GetTypeData(ClassTypeInfo);   
          if   ClassTypeData.PropCount   <>   0   then   
          begin   
              GetMem(PropList,   SizeOf(PPropInfo)   *   ClassTypeData.PropCount);   
              try   
                  GetPropInfos(AClass.ClassInfo,   PropList);   
                  for   I   :=   0   to   ClassTypeData.PropCount   -   1   do   
                      if   not   (PropList[I]^.PropType^.Kind   =   tkMethod)   then   
                          AStrings.Add(Format('%s:   %s(%s)',   
                              [PropList[I]^.Name,   PropList[I]^.PropType^.Name,   
                                  VarToStr(GetPropValue(AClass,   PropList[I]^.Name))]));   
                  NumProps   :=   GetPropList(AClass.ClassInfo,   [tkMethod],   PropList);   
                  if   NumProps   <>   0   then   
                  begin   
                      AStrings.Add('');   
                      AStrings.Add('       EVENTS       ================   ');   
                      AStrings.Add('');   
                  end;   
        
                  for   I   :=   0   to   NumProps   -   1   do   
                  begin   
                      AStrings.Add(Format('%s:   %s(%s)',   [PropList[I]^.Name,   
                          PropList[I]^.PropType^.Name,   
                          AClass.MethodName(GetMethodProp(AClass,   PropList[I]).Code)]));   
                  end;   
              finally   
                  FreeMem(PropList,   SizeOf(PPropInfo)   *   ClassTypeData.PropCount);   
              end;   
          end;   
      end;   
      

  2.   

    参考
    Delphi 的RTTI机制浅探
      

  3.   

    恩,就是RTTI的机制了。
    给一段以前参考别人代码写的得到类事件函数头的代码
    function GetEventHandle(EventObj: TObject; EventName: string): string;
    type
      TParamRec = record
      Flags: TParamFlags;
      ParamName: ShortString;
      TypeName: ShortString;
      end;
      pParamRec = ^TParamRec;
      function GetFlags(Flags: TParamFlags): string;
      var
        i: integer;
      begin
         for i := 0 to 5 do
         begin
           if i = 3 then Continue;
           if TParamFlag(i) in Flags then
            Result := Copy(GetEnumName(TypeInfo(TParamFlag),i),3,MaxInt) + ' ';
         end;
      end;
    var
      propInfo: PPropInfo;
      TypeData: PTypeData;
      pTypeStr: PShortString;
      Param: pParamRec;
      i: integer;
    begin
      propInfo := GetPropInfo(EventObj,EventName);
      if propInfo <> nil then
      begin
         TypeData := GetTypeData(PropInfo^.PropType^);
         if TypeData <> nil then
         begin
           Param := pParamRec(@TypeData^.ParamList);
           result := 'Procedure '+PropInfo^.Name+'(';
           for i := 0 to TypeData^.ParamCount - 1 do
           begin
              result := Result + GetFlags(Param^.Flags) + Param^.ParamName;
              pTypeStr := pShortString(Integer(@Param^.ParamName)+Length(Param^.ParamName)+1);
              result := Result + ': '+ pTypeStr^ ;
              if i <> TypeData^.ParamCount - 1 then
                result := result + '; '
              else result := Result + ');';
              Param := PParamRec(Integer(@Param^.ParamName)+Length(Param^.ParamName) + Length(pTypeStr^)+2);
           end;
         end;
      end;
    end;
      

  4.   

    要搞懂RTTI机制,我以前做个一个属性编辑器,用来做自定义界面和报表,比较复杂,不是一两个篇幅可以描述清楚的.
      

  5.   

    昨天和今天早上看了下RTTI机制,还有一些不明白的地方;就是如何把相同事件列出来
    并把所有的相关事件列出来(例:Tbutton.onClick列表下可能有Button1Click,Button2Click,Button3Click等).
    请各位CSND高手解答一下,谢谢.
      

  6.   

    我想应该可根据参数来进行判断啊!
    我在8楼已经将一个事件过程的过程头如
    procedure BtnClick(Sender: TObject);
    这样的过程头写出来了,应该匹配起来不是难事吧!