如题,例如:我要设置Form的Caption但“Caption”这个属性我事先是不知道的,要从一个变量中取出来。这种情况我如何做呢?份不够可以再加.

解决方案 »

  1.   

    我从《Delphi5开发人员指南》截下来的。希望对你有些参考作用。
    Function CreateAClass(Const AClassName:String):TObject;
    var
      C :TFormClass;
      SomeObject :TObject;
    begin
      C :=TFormClass(FindClass(AClassName));
      SomeObject :=C.Create(Nil);
      Result :=SomeObject;
    end;procedure GetBaseClassInfo(AClass :TObject;AStrings :TStrings);
    var
      ClassTypeInfo :PTypeInfo;
      ClassTypeData :PTypeData;
      EnumName:String;
    begin
      ClassTypeInfo :=AClass.ClassInfo;
      ClassTypeData :=GetTypeData(ClassTypeInfo);
      with AStrings do
      begin
        Add(Format('Class Name: %s',[ClassTypeInfo.Name]));
        EnumName :=GetEnumName(TypeInfo(TTypeKind),Integer(ClassTypeInfo.Kind));
        Add(Format('Kind: %s',[EnumName]));
        Add(Format('Size: %d',[AClass.InstanceSize]));
        Add(Format('Defined in  %s.Pas',[ClassTypeData.UnitName]));
        Add(Format('Num Properties  %d',[ClassTypeData.PropCount]));
      end;
    end;
    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(PpropList)*ClassTypeData.PropCount);
        try
          GetPropInfos(AClass.ClassInfo,proplist);
          for i:=0 to ClassTypeData.PropCount-1 do
            if proplist[i]^.PropType^.Kind<>tkMethod then
              AStrings.Add(Format('%s:  %s',[proplist[i]^.Name,proplist[i]^.PropType^.Name]));      NumProps :=GetPropList(AClass.ClassInfo,[tkMethod],proplist);
          AStrings.Add('');
          AStrings.Add(' Event  ======  ');
          AStrings.Add('');      for i:=0 to NumProps do
            AStrings.Add(format('%s: %s',[proplist[i]^.Name,proplist[i]^.propType^.Name]));    finally
          FreeMem(proplist,sizeof(Pproplist)*ClassTypeData.PropCount);
        end;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      listbox3.Items.Add('TApplication');
      listbox3.Items.Add('TButton');
      listbox3.Items.Add('TForm');
      listbox3.Items.Add('TListBox');
      listbox3.Items.Add('TPaintBox');
      listbox3.Items.Add('TTimer');
      listbox3.Items.Add('TFindDialog');
      listbox3.Items.Add('TOpenDialog');
      listbox3.Items.Add('TComponent');
    end;procedure TForm1.ListBox3Click(Sender: TObject);
    var
      somecomp:TObject;
    begin
      listbox1.Items.Clear;
      listbox2.Items.Clear;  somecomp :=CreateAClass(listbox3.Items[listbox3.itemindex]);
      try
        GetBaseClassInfo(somecomp,listbox1.Items);
        //GetClassAncestry(somecomp,listbox1.Items);
        GetClassProperties(somecomp,listbox2.Items);
      finally
        somecomp.Free;
      end;
    end;
      

  2.   

    谢谢dht2003(海),我先研究一下。
    大家有好办法都可以贴上来讨论。
      

  3.   


    自己看 $(delphi)\source\Rtl\Common\TypInfo.pas下面是一个简单的例子,是一些常用的函数和类型,可以直接用SetPropValue函数procedure SetCaptionProp(Instance : TObject; const s : string);
    var
      PropInfo: PPropInfo;
    begin
      PropInfo := GetPropInfo(Instance, 'caption');
      if PropInfo = nil then     //没有caption属性
        exit;
      if PropInfo^.SetProc = nil then  //只读属性
        exit;
      if not (PropInfo^.PropType^^.Kind in [tkString, tkLString, tkWString]) then
        exit;     //类型不匹配  SetPropValue(Instance, 'caption', s);
    end;