uses
..., TypInfo;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
aComponent: TComponent;
PropInfo: PPropInfo;
ViewText, ViewHint: string;
begin
ViewText := 'Hello world'; // for debug use, you should comment these 2 lines
ViewHint := 'the hint'; // and then implement GetInfoByName method.
for i := 0 to ComponentCount - 1 do
begin
aComponent := Components[i];
PropInfo := GetPropInfo(aComponent.ClassInfo, 'Caption');
if not Assigned(PropInfo) then // Caption property of aComponent NOT exists
begin
// turn to search Text property
PropInfo := GetPropInfo(aComponent.ClassInfo, 'Text');
if not Assigned(PropInfo) then // Text property of aComponent NOT exists
Continue;
end;
// comes here, either Caption or Text property must exists
// first, find the ViewText and ViewHint of the aComponent by the name
// GetInfoByName(aComponent.Name, ViewText, ViewHint);
SetStrProp(aComponent, PropInfo, ViewText);
// then, search Hint property
PropInfo := GetPropInfo(aComponent.ClassInfo, 'Hint');
if Assigned(PropInfo) then
SetStrProp(aComponent, PropInfo, ViewHint);
end;
end;
****************************************************************************************
Function IsAPropExist(AInst: TObject; const PropName: string): Boolean;
var
PropInfo: PPropInfo;
begin
Result := False;
PropInfo := GetPropInfo(AInst, PropName);
if PropInfo <> nil then
Result := PropInfo.Name = PropName;
end;
procedure SetLang;
var
i: Integer;
StrValue: string;
StrObj: string;
StrCaption :String;
begin
for i := 0 to Self.ComponentCount - 1 do
begin
if IsAPropExist(Self.Components[i], 'Caption') then
begin
StrValue := VarToStr(GetPropValue(self.Components[i], 'Caption'));
if (StrValue = '') or (StrValue = '-') then
Continue;
StrCaption := rwIni1.iReadString(self.ClassName, Self.Components[i].Name,'');
if Trim(strCaption) <> '' then Continue;
rwIni1.iWriteString(self.ClassName, Self.Components[i].Name, StrValue);
end;
end;
end;
procedure ReadLang;
var
i: Integer;
StrValue: string;
StrObj: string;
StrCaption: string;
begin
for i := 0 to Self.ComponentCount - 1 do
begin
if IsAPropExist(Self.Components[i], 'Caption') then
begin
StrValue := VarToStr(GetPropValue(self.Components[i], 'Caption'));
StrObj := Trim(Self.Components[i].Name);
if (StrObj = '') or (strObj = '-') then
Continue;
StrCaption := rwIni1.iReadString(self.ClassName, Self.Components[i].Name, StrValue);
if Trim(strCaption)='' then Continue;
SetPropValue(Self.Components[i], 'Caption', StrCaption);
end;
end;
end;
枚舉並設置界面的Caption之多語言方法
最新推荐文章于 2018-12-07 21:15:00 发布