Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Mostrar las propiedades de un componente en runtime (https://www.clubdelphi.com/foros/showthread.php?t=80440)

Neftali [Germán.Estévez] 08-06-2006 12:52:10

Mostrar las propiedades de un componente en runtime
 
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:

Código Delphi [-]
//: 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:

Código Delphi [-]
   Memo1.Lines.BeginUpdate;
   Memo1.Lines.Clear;
   ListComponentProperties(Button1, Memo1.Lines);
   Memo1.Lines.EndUpdate;

Se debe añadir la Unit TypInfo al USES.


La franja horaria es GMT +2. Ahora son las 02:22:11.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi