All visual controls have a Font
property, but it is protected
at the TControl
layer, and not all derived controls promote it to published
. If you are interested in only controls that have a published Font
than you have to use RTTI to test them, eg:
uses
..., TypInfo;
var
Ctrl: TControl;
i: Integer;
begin
for i := 0 to ContainerControl.ControlCount - 1 do
begin
Ctrl := ContainerControl.Controls[i];
if IsPublishedProp(Ctrl, 'Font') then
TFont(GetObjectProp(Ctrl, 'Font', TFont)).Size := 8;
end;
end;
Alternatively:
uses
..., TypInfo;
var
Ctrl: TControl;
Prop: PPropInfo;
i: Integer;
begin
for i := 0 to ContainerControl.ControlCount - 1 do
begin
Ctrl := ContainerControl.Controls[i];
Prop := GetPropInfo(Ctrl, 'Font', [tkClass]);
if Prop <> nil then
TFont(GetObjectProp(Ctrl, Prop, TFont)).Size := 8;
end;
end;
Alternatively, in Delphi 2010 and later only:
uses
..., System.Rtti;
var
Ctrl: TControl;
Ctx: TRttiContext;
Prop: TRttiProperty;
i: Integer;
begin
Ctx := TRttiContext.Create;
try
for i := 0 to ContainerControl.ControlCount - 1 do
begin
Ctrl := ContainerControl.Controls[i];
Prop := Ctx.GetType(Ctrl.ClassType).GetProperty('Font');
if (Prop <> nil) and (Prop.Visibility = TMemberVisibility.mvPublished) then
TFont(Prop.GetValue(Ctrl).AsObject).Size := 8;
end;
finally
Ctx.Free;
end;
end;