Update fonts recursively in Delphi form

I am trying to iterate over all the controls on a form and enable ClearType font smoothing. Something like that:

procedure TForm4.UpdateControls(AParent: TWinControl); var I: Integer; ACtrl: TControl; tagLOGFONT: TLogFont; begin for I := 0 to AParent.ControlCount-1 do begin ACtrl:= AParent.Controls[I]; // if ParentFont=False, update the font here... if ACtrl is TWinControl then UpdateControls(Ctrl as TWinControl); end; end; 

Now, is there an easy way to check if ACtrl a Font property, so I can pass Font.Handle to something like:

 GetObject(ACtrl.Font.Handle, SizeOf(TLogFont), @tagLOGFONT); tagLOGFONT.lfQuality := 5; ACtrl.Font.Handle := CreateFontIndirect(tagLOGFONT); 

Thanks in advance.

+4
source share
4 answers

You are using the TypInfo module, more specifically, the IsPublishedProp and GetOrdProp methods.

In your case, it will be something like:

 if IsPublishedProp(ACtrl, 'Font') then ModifyFont(TFont(GetOrdProp(ACtrl, 'Font'))) 

A snippet from one of my libraries that should put you on the right path:

 function ContainsNonemptyControl(controlParent: TWinControl; const requiredControlNamePrefix: string; const ignoreControls: string = ''): boolean; var child : TControl; iControl: integer; ignored : TStringList; obj : TObject; begin Result := true; if ignoreControls = '' then ignored := nil else begin ignored := TStringList.Create; ignored.Text := ignoreControls; end; try for iControl := 0 to controlParent.ControlCount-1 do begin child := controlParent.Controls[iControl]; if (requiredControlNamePrefix = '') or SameText(requiredControlNamePrefix, Copy(child.Name, 1, Length(requiredControlNamePrefix))) then if (not assigned(ignored)) or (ignored.IndexOf(child.Name) < 0) then if IsPublishedProp(child, 'Text') and (GetStrProp(child, 'Text') <> '') then Exit else if IsPublishedProp(child, 'Lines') then begin obj := TObject(cardinal(GetOrdProp(child, 'Lines'))); if (obj is TStrings) and (Unwrap(TStrings(obj).Text, child) <> '') then Exit; end; end; //for iControl finally FreeAndNil(ignored); end; Result := false; end; { ContainsNonemptyControl } 
+6
source

There is no need to use RTTI for this. Each descendant of TControl has a Font property. At the TControl level, its visibility is protected, but you can use this workaround to access it:

 type THackControl = class(TControl); ModifyFont(THackControl(AParent.Controls[I]).Font); 
+5
source

Another thing worth mentioning. Each control has a ParentFont property, which - if set - allows the selection of the form font to pulse to each control. I try to get ParentFont installed as soon as possible, which also makes it easier to create themes in accordance with the current OS.

Anyway, you don’t have to do anything to enable ClearType anti-aliasing? This should happen automatically if you use the TrueType font and the user has enabled the Cleartype effect.

0
source

Here's a C ++ Builder example. Answer by TOndrej :

 struct THackControl : TControl { __fastcall virtual THackControl(Classes::TComponent* AOwner); TFont* Font() { return TControl::Font; }; }; for(int ControlIdx = 0; ControlIdx < ControlCount; ++ControlIdx) { ((THackControl*)Controls[ControlIdx])->Font()->Color = clRed; } 
0
source

Source: https://habr.com/ru/post/1277369/


All Articles