Board index » delphi » Font Property

Font Property

Hi,

How can I dectect if an component has a TFont as a property,
I want to check all mij components on my form and If they have a TFont
property I want to change the size,
My delphi Help says that a TControl component has a TFont property, but my
delphi 3 compiler disagree. So I can't check if form1.components[x] is
TControl and
access on this way the font property.

Does anyone have a clue?

Thanks Simon

 

Re:Font Property


On 24 Oct 1998 16:53:13 GMT, "Simon Zwarts"

Quote
<(S.Zwa...@student.utwente.nl)> wrote:
>How can I dectect if an component has a TFont as a property,
>I want to check all mij components on my form and If they have a TFont
>property I want to change the size,
>My delphi Help says that a TControl component has a TFont property, but my
>delphi 3 compiler disagree. So I can't check if form1.components[x] is
>TControl and
>access on this way the font property.

A TControl does have a Font property, but it's protected.  Some TControl
descendants make it visible and others don't.  You can do this:

  if (TheComponent is TControl) then
    TEdit(TheComponent).Font.Name := 'Stempel Fruitbat';

This is illegal and horrible and wrong, and in Delaware can get you two
to five years, but it works.  There's a slight chance you'll run into a
control that kept Font hidden for a good reason and will do something
wrong if you change it, but I've never seen one.

Quote
>Does anyone have a clue?

Only about Delphi.

--
Report all {*word*201} mail to your Potsmaster.

Re:Font Property


In article <36322d21.14200...@netnews.worldnet.att.net>,

Quote
drifk...@acm.deleteme.org (David Rifkind) writes:
>  if (TheComponent is TControl) then
>    TEdit(TheComponent).Font.Name := 'Stempel Fruitbat';

>This is illegal and horrible and wrong

Can you explain why you think it is illegal and horrible and wrong.

If the component is a TControl then the Font property _is_ incorporated into
the component(its not a TMacAvity <g>) - but the compiler will not let you
access it. Any descendant of TControl which un-protects the Font property is a
valid and allowable type-cast to allow the compiler to "unlock the door".

However I admit that the way it is being used to access objects which are not
TEdit may not be good coding practice, but the programming technique _is_
acceptable

A better way of using the type-casting technique to access the Font property is
to declare a descendant of TControl and cast the component as that. Delphi then
will allow access to protected items within the same unit.  My personal
convention is to insert some indication of the reason I have declared the
descendant in its name :-

type
  TFontControl = class(TControl)

then later
  if (TheComponent is TControl) then
    TFontControl(TheComponent).Font.Name := 'Stempel Fruitbat';

Another alternative might be to use the technique of accessing the run-time
type information similar to an example by Ray Lischner in his book "Secrets of
Delphi 2" on p189

Alan Lloyd
alangll...@aol.com

Re:Font Property


Hi Simon,

The way Ray Lischner describes (chapter 7) in his Secrets of Delphi is, I
suppose, the safest way. If you add 'TypInfo' to the uses-clause of your
unit, then the following works well:

procedure TForm1.Button1Click(Sender: TObject);
var
 I,Size:Integer;
 Font:TFont;
 PropInfo:PPropInfo;{see Delphi Help}
begin
 for I:=0 to ControlCount-1 do begin
  PropInfo:=GetPropInfo(Controls[I].ClassInfo,'Font');
  {PropInfo <> Nil if Controls[I] has a Font-property.}
  if PropInfo<>Nil then begin
   Font:=Pointer(GetOrdProp(Controls[I],PropInfo));
   {Gives you the Font-property of Controls[I]}
   if Font is TFont {be sure of that!} then begin
    PropInfo:=GetPropInfo(Font.ClassInfo,'Size');{or 'Height'}
    Size:=GetOrdProp(Font,PropInfo);
    {Gives you the Font-size, if necessary}
    SetOrdProp(Font,PropInfo,12);
    {Sets the Font-size}
   end;
  end;
 end;
end;

And even the following one works well, but I don't know how many years you
will get for that in Delaware:

procedure TForm1.Button2Click(Sender: TObject);
var
 I:Integer;
 Font:TFont;
 PropInfo:PPropInfo;
begin
 for I:=0 to ControlCount-1 do begin
  PropInfo:=GetPropInfo(Controls[I].ClassInfo,'Font');
  if PropInfo<>Nil then begin
   Font:=Pointer(GetOrdProp(Controls[I],PropInfo));
   if Font is TFont then Font.Size:=Font.Size+1;
  end;
 end;
end;

Good luck,

Hugo Kuiper

Re:Font Property


On 25 Oct 1998 08:02:50 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>In article <36322d21.14200...@netnews.worldnet.att.net>,
>drifk...@acm.deleteme.org (David Rifkind) writes:

>>  if (TheComponent is TControl) then
>>    TEdit(TheComponent).Font.Name := 'Stempel Fruitbat';

>>This is illegal and horrible and wrong

>Can you explain why you think it is illegal and horrible and wrong.

I suppose it make me feel racy to think I'm cheating that way.  Calling
it acceptable practice takes away all of the fun.  I use both methods,
and prefer yours, but for some reason didn't think of it at the time.

--
Report all {*word*201} mail to your Potsmaster.

Other Threads