PDA

Ver la Versión Completa : Mostrar las propiedades de un componente en runtime


Neftali [Germán.Estévez]
08-06-2006, 13:52:10
Podemos listar todas las propiedades published de un componente utilizando RTTI. Para ver el funcionamiento podemos crear un formulario con un componente TMemo y un Botón.

El procedimieno que utilizamos para obtener las propiedades es el siguiente:


//: Lista las propiedades/eventos de un componente.
procedure ListComponentProperties(Componente:TComponent; Strings: TStrings);
var
Count, Size, j: Integer;
lista: PPropList;
PropInfo: PPropInfo;
PropOrEvent, PropValue: String;
begin
// Obtener el número de props. -- Number of props.
Count := GetPropList(Componente.ClassInfo, tkAny, nil);
Size := Count * SizeOf(Pointer);
// reservar memoria
GetMem(lista, Size);
// Bloque de proteccion -- protection block
try
Count := GetPropList(Componente.ClassInfo, tkAny, lista);
// recorrer la lista de prop.
for j := 0 to Count - 1 do begin
PropInfo := lista^[j];
// Tipo?
if (PropInfo^.PropType^.Kind in tkMethods) then begin
PropOrEvent := 'Event'
end
else begin
PropOrEvent := 'Property';
end;
// Obtener el valor de la propiedad -- Get the prop. value
PropValue := VarToStr(GetPropValue(Componente, PropInfo^.Name));
// Añadirla a la lista
Strings.Add(Format('[%s] Nombre:%s Tipo:%s Valor:%s',
[PropOrEvent, PropInfo^.Name, PropInfo^.PropType^.Name, PropValue]));
end;
finally
FreeMem(lista);
end;
end;


Para utilizarlo podemos usar el siguiente código:


Memo1.Lines.BeginUpdate;
Memo1.Lines.Clear;
ListComponentProperties(Button1, Memo1.Lines);
Memo1.Lines.EndUpdate;


Se debe añadir la Unit TypInfo al USES.