Author: Jonas Bilinkevicius
I would like to change the font color on all components on a form at runtime (and
the components owned by the components etc). I devised a recursive algorithm using
RTTI that accepts a TComponent as a parameter. It works to some extent, but I still
have to use 'if' statements to cast the object to a particular descendant,
resulting in about 30 lines of code to test for all of the components I use. Also,
some objects (TColumnTitle), are not descended from TComponent, even though they
have a font property.
Answer:
This may do the trick (with D6 and maybe D5):
1 uses2 TypInfo;
3 4 { ... }5 var6 i: integer;
7 aFont: TFont;
8 begin9 for i := 0 to aComponent.ComponentCount - 1 do10 begin11 aFont := TFont(GetOrdProp(aComponent.Components[i], 'Font'));
12 if assigned(aFont) then13 aFont.Color := clWhite;
14 end;
15 end;
16 17 18 //With D4:19 20 { ... }21 var22 i: integer;
23 aFont: TFont;
24 pi: PPropInfo;
25 begin26 for i := 0 to aComponent.ComponentCount - 1 do27 begin28 pi := GetPropInfo(aComponent.Components[i].ClassInfo, 'Font');
29 if assigned(pi) then30 TFont(GetOrdProp(aComponent.Components[i], pi)).Color := clWhite;
31 end;
32 end;