Board index » delphi » Q: problem with class casting and FieldAddress method !?

Q: problem with class casting and FieldAddress method !?

hi,

i am trying to modify the font name of visual components for our far east
application edition.
i want to insert the change at the end of  TCustomForm.Create constructor
within IFDEF clause.

1. when i am trying the following code i get compile error :
    Undeclared identifier: 'Font'
and the cursor stands after the word Font before the word Name.

What is wrong here ???

{$IFDEF SUPPORT_FAREAST}
  for i := 0 to (ComponentCount - 1) do
  begin
    if Components[i] is TControl then
    begin
      TControl(Components[i]).Font.Name := 'arial-fareast';
    end;
  end;
{$ENDIF}

2. i triend another way with FieldAddress method but i always got nil , why
?

  for i := 0 to (ComponentCount - 1) do
  begin
    if Components[i] is TControl then
    begin
        FontPtr := Components[i].FieldAddress('Font');
        if FontPtr <> nil then
                TFont(FontPtr).name := 'arial-fareast';
    end;
  end;

3. i trien to take advantage of the ParentFont property but the database
controls didn't change thier fonts , why

  Font.Name := 'arial-fareast';
  Perform(CM_PARENTFONTCHANGED, 0, 0);

thanx in advance

rafi newman

 

Re:Q: problem with class casting and FieldAddress method !?


Quote
> i am trying to modify the font name of visual components for our far east
> application edition.
> i want to insert the change at the end of  TCustomForm.Create constructor
> within IFDEF clause.

> 1. when i am trying the following code i get compile error :
>     Undeclared identifier: 'Font'
> and the cursor stands after the word Font before the word Name.

> What is wrong here ???

> {$IFDEF SUPPORT_FAREAST}
>   for i := 0 to (ComponentCount - 1) do
>   begin
>     if Components[i] is TControl then
>     begin
>       TControl(Components[i]).Font.Name := 'arial-fareast';
>     end;
>   end;
> {$ENDIF}

TControl does not have a public or published Font property. Use run-time type
information and functions from the TypInfo unit:

Uses TypInfo;

Function AssignFontName( anObj: TObject; Const fname: String ):
  Boolean;
Var
  PInfo: PPropInfo;
  aFont: TFont;
Begin
  (* try to get a pointer to the property information for a property with the
     name 'Font'. TObject.ClassInfo returns a pointer to the RTTI table,
which
     we need to pass to GetPropInfo *)
  PInfo := GetPropInfo( anObj.ClassInfo, 'font' );
  Result := PInfo <> Nil;
  If result Then
    (* found a property with this name, check if it has the correct type *)
    If (PInfo^.Proptype^.Kind = tkClass) and
        GetTypeData(PInfo^.Proptype^)^.ClassType.InheritsFrom(TFont) Then
Begin
      aFont := TFont(GetOrdProp( anObj, PInfo ));
      aFont.Name := fname
    End
    Else Begin
      (* nope, wrong type, complain *)
      Result := False;
      ShowMessage( 'Property Font is not of type TFont!');
    End;
End;

procedure TForm1.BtnTestClick(Sender: TObject);
Var
  i: Integer;
begin
  For i:= 0 To ComponentCount-1 Do
    AssignFontName( Components[i], 'Symbol' );
end;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Other Threads