Author: Tomas Rutkauskas
How to change the font of all controls on a form at runtime
Answer:
By default all controls have ParentFont = true, so if you did not change that for
specific controls you could just change the forms Font property, e.g. in code
attached to the Screen.OnActiveFormChange event. If you cannot rely on all controls
having Parentfont = true you would have to loop over all controls on the form and
set the font property for each or at least for those that have ParentFont set to
false. You can use the routines from unit TypInfo for that, they allow you to
access published properties by name. The code, again sitting in a handler for
Screen.onActiveFormChange, would be something like this:
1 ModifyFontsFor(Screen.ActiveControl);
2 3 //where4 5 procedure ModifyFontsFor(ctrl: TWinControl);
6 7 procedure ModifyFont(ctrl: TControl);
8 var9 f: TFont;
10 begin11 if IsPublishedProp(ctrl, 'Parentfont') and (GetOrdProp(ctrl, 'Parentfont') =
12 Ord(false)) and IsPublishedProp(ctrl, 'font') then13 begin14 f := TFont(GetObjectProp(ctrl, 'font', TFont));
15 f.Name := 'Symbol';
16 end;
17 end;
18 19 var20 i: Integer;
21 begin22 ModifyFont(ctrl);
23 for i := 0 to ctrl.controlcount - 1 do24 if ctrl.controls[i] is TWinControl then25 ModifyFontsfor(TWinControl(ctrl.controls[i]))
26 else27 Modifyfont(ctrl.controls[i]);
28 end;
Remember to add TypInfo to your uses clause.