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;
>>另有人说可以使用rtti,不知道为何物! 就是 Run-Time Type Information
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;
cp := FindComponent(ComName); 的后面不妨加入一个判断 if cp=nil then exit; 收藏
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;
Loop: Integer;
PropInfo: PPropInfo;
cp: TComponent;
begin
cp := FindComponent('Button1');
PropInfo := GetPropInfo(cp.ClassInfo, 'Caption');
if Assigned(PropInfo) then
SetStrProp(cp, PropInfo, 'test');
end;
就是 Run-Time Type Information
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;
的后面不妨加入一个判断
if cp=nil then
exit;
收藏
比如 TObject的函数.function ClassType: TClass;
class function ClassName: ShortString;
class function ClassNameIs(const Name: string): Boolean;
class function ClassParent: TClass;
class function ClassInfo: Pointer;