Code:

function GetProperty(AControl: TPersistent; AProperty: String): PPropInfo;

var

i: Integer;

props: PPropList;

typeData: PTypeData;

begin

Result := nil;

if (AControl = nil) or (AControl.ClassInfo = nil) then

   Exit;

typeData := GetTypeData(AControl.ClassInfo);

if (typeData = nil) or (typeData^.PropCount = 0) then

   Exit;

GetMem(props, typeData^.PropCount * SizeOf(Pointer));

try

   GetPropInfos(AControl.ClassInfo, props);

   for i := 0to typeData^.PropCount - 1do

   begin

     with Props^[i]^ do

       if (Name = AProperty) then

         result := Props^[i];

   end;

finally

   FreeMem(props);

end;

end;

 

 

procedure TForm1.Button1Click(Sender: TObject);

var

propInfo: PPropInfo;

begin

PropInfo := GetProperty(Button1.Font, 'Name');

if PropInfo <> nilthen

   SetStrProp(Button1.Font, PropInfo, 'Arial');

end;

 

 

You can use RTTI to do this. Here is how to change a particular component:

Code:

procedure TForm1.BtnClick(Sender: TObject);

var

p: PPropInfo;

f: TFont;

begin

f := TFont.Create;

{Setup the font properties}

f.Name := 'Arial';

p := GetPropInfo(Sender.ClassInfo, 'Font');

if Assigned(p) then

   SetOrdProp(Sender, p, Integer(f));

f.Free;

end;

To get at all the forms loop through the Screen global variable. For each form loop through its Components list calling the above procedure (or something close). If you only create your components at design time that is it. If you create some at runtime and the owner is not the form, then for each component loop through its Components list recursively to get at all the owned components.

 I am building a routine that checks our forms for validity before deploying them. I would like to use some kind of structure that tests if a component type has access to a certain property, something like: " if (self.Controls[b] has Tag) then ...". Can anyone offer suggestions?

Here's an example of setting a string property for a component if it exists and another for an integer property:

Code:

{

Copyright © 1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira

}

procedure SetStringPropertyIfExists(AComp: TComponent; APropName: String;

AValue: String);

var

PropInfo: PPropInfo;

TK: TTypeKind;

begin

PropInfo := GetPropInfo(AComp.ClassInfo, APropName);

if PropInfo <> nilthen

begin

   TK := PropInfo^.PropType^.Kind;

   if (TK = tkString) or (TK = tkLString) or (TK = tkWString) then

     SetStrProp(AComp, PropInfo, AValue);

end;

end;

 

 

procedure SetIntegerPropertyIfExists(AComp: TComponent; APropName: String;

AValue: Integer);

var

PropInfo: PPropInfo;

begin

PropInfo := GetPropInfo(AComp.ClassInfo, APropName);

if PropInfo <> nilthen

begin

   if PropInfo^.PropType^.Kind = tkInteger then

     SetOrdProp(AComp, PropInfo, AValue);

end;

end;

 Tip by Xavier Pacheco

Добавить комментарий

Не использовать не нормативную лексику.

Просьба писать ваши замечания, наблюдения и все остальное,
что поможет улучшить предоставляемую информацию на этом сайте.

ВСЕ КОММЕНТАРИИ МОДЕРИРУЮТСЯ ВРУЧНУЮ, ТАК ЧТО СПАМИТЬ БЕСПОЛЕЗНО!


Защитный код
Обновить