如'Button1.Caption'
如何映射为Button1.Caption,可以对其赋值Button1.Caption:=''wwww'
Button1.Caption,类名属性名都不确定另有人说可以使用rtti,不知道为何物!
请给出完整程序代码!

解决方案 »

  1.   

    uses TypInfo;var
      Loop: Integer;
      PropInfo: PPropInfo;
      cp: TComponent;
    begin
        cp := FindComponent('Button1');
        PropInfo := GetPropInfo(cp.ClassInfo, 'Caption');
        if Assigned(PropInfo) then
          SetStrProp(cp, PropInfo, 'test');
    end;
      

  2.   

    >>另有人说可以使用rtti,不知道为何物!
    就是 Run-Time Type Information
      

  3.   

    uses TypInfo;function TForm1.SetControlProp(ComStr, value: string): boolean;
    var ComName, ComProp: string;
        i: integer;
      Loop: Integer;
      PropInfo: PPropInfo;
      cp: TComponent;
    begin
      i := Pos('.', ComStr);
      if i > 0 then
      begin
        ComName := copy(ComStr, 1, i-1);
        ComProp := Copy(Comstr, i+1, 1000);
        cp := FindComponent(ComName);
        PropInfo := GetPropInfo(cp.ClassInfo, ComProp);
        if Assigned(PropInfo) then
          SetStrProp(cp, PropInfo, value);
      end;
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
     SetControlProp('button1.caption', 'test'); //設置button1的caption 為 test
    end;
      

  4.   

    cp := FindComponent(ComName);
    的后面不妨加入一个判断
    if cp=nil then
     exit;
    收藏
      

  5.   

    rtti 就是可以获取 类的运行时信息 这是编译器<语言>的一种机制 都从TObject继承
    比如 TObject的函数.function ClassType: TClass;
        class function ClassName: ShortString;
        class function ClassNameIs(const Name: string): Boolean;
        class function ClassParent: TClass;
        class function ClassInfo: Pointer;