function HasProperty(const AObject: TObject;const APropName:String):Boolean;
var
PropInfo:PPropInfo;
begin
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
Result:=Assigned(PropInfo);
end;
function GetObjectProperty(
const AObject : TObject;
const APropName : string
):TObject;
var
PropInfo:PPropInfo;
begin
Result := nil;
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind = tkClass) then
Result := GetObjectProp(AObject,PropInfo);
end;
procedure SetFontName(Component: TComponent; FontName: string);
var
PropInfo: PPropInfo;
lCtl:TControl;
lFont:TFont;
begin
if Component = nil then exit;
PropInfo := GetPropInfo(Component, 'FONT');
if PropInfo = nil then exit;
if (PropInfo.PropType^.Name <> 'TFont') then Exit;
lCtl:=TWinControl(Component);
lFont:=TFont(GetObjectProperty(lCtl,'FONT'));
lFont.Name:=FontName;
var
PropInfo:PPropInfo;
begin
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
Result:=Assigned(PropInfo);
end;
function GetObjectProperty(
const AObject : TObject;
const APropName : string
):TObject;
var
PropInfo:PPropInfo;
begin
Result := nil;
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind = tkClass) then
Result := GetObjectProp(AObject,PropInfo);
end;
procedure SetFontName(Component: TComponent; FontName: string);
var
PropInfo: PPropInfo;
lCtl:TControl;
lFont:TFont;
begin
if Component = nil then exit;
PropInfo := GetPropInfo(Component, 'FONT');
if PropInfo = nil then exit;
if (PropInfo.PropType^.Name <> 'TFont') then Exit;
lCtl:=TWinControl(Component);
lFont:=TFont(GetObjectProperty(lCtl,'FONT'));
lFont.Name:=FontName;
end;
procedure TForm1.FormCreate(Sender: TObject);
Var
lComponent: TComponent;
i: Integer;
lFontName: String;
Begin
lFontName:='黑体';//'宋体';
For i := 0 To Self.ComponentCount - 1 Do
Begin
lComponent := Self.Components[i];
if HasProperty(lComponent, 'FONT') then
SetFontName(lComponent,lFontName);
End;
end;
本文介绍了一种在Delphi中检查对象是否具有特定属性的方法,并提供了设置对象字体名称的实用函数。通过使用GetPropInfo函数获取属性信息,可以实现对组件字体属性的统一设置。
2万+

被折叠的 条评论
为什么被折叠?



