下面是一个通用过程,根据INI文件中的资源改变界面语言(仅Caption和Hint属性)
参数一:为一Component,一般为TForm,此过程将循环列举其子Component并查找是否具有相关属性,
有的话则从INI文件读取信息并设置属性
参数二:为INI文件全名
//引用TypInfo单元
//声明的资源,默认的,防止变量出现空值
resourcestring
S_Menu = ''Menu'';
S_AppName = ''Window View Tool'';
S_Null = ''(Null)'';
S_About = ''&About'';
S_Warning = ''Warning'';
S_Confirm = ''Confirm'';
S_Error = ''Error'';
S_Information = ''Information'';
//声明的常量
const
SCaption = ''Caption'';
SHint = ''Hint'';
SIniAppName = ''Window View Tools'';
SIniLanguage = ''Language'';
SLanguageName = ''LanguageName'';
{-----------------------------------------------------------------------------
Procedure: LoadLanguage
Author: ysai
Date: 2003-7-20
Arguments: AComponent:TComponent;AFileName:string=''''
-----------------------------------------------------------------------------}
procedure LoadLanguage(
const AComponent : TComponent;
const AFileName : string = ''''
);
//设置语言
function PropertyExists(
const AObject : TObject;
const APropName : String
):Boolean;
//判断一个属性是否存在
var
PropInfo:PPropInfo;
begin
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
Result:=Assigned(PropInfo);
end;
function SetStringPropertyIfExists(
const AObject : TObject;
const APropName : string;
const AValue : string
):Boolean;
//给字符串属性赋值,如果存在
var
PropInfo:PPropInfo;
begin
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo.PropType^.Kind in [tkString, tkLString, tkWString]) then
begin
SetStrProp(AObject,PropInfo,AValue);
Result:=True;
end else
Result:=False;
end;
function GetStringProperty(
const AObject : TObject;
const APropName : string
):String;
//取得字符串属性值
var
PropInfo:PPropInfo;
begin
Result := '''';
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind in [tkString, tkLString, tkWString]) then
Result := GetStrProp(AObject,PropInfo);
end;
function SetIntegerPropertyIfExists(
const AObject : TObject;
const APropName : string;
const AValue : integer
):Boolean;
//给整型属性赋值,如果存在
var
PropInfo:PPropInfo;
begin
PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
if Assigned(PropInfo) and
(PropInfo^.PropType^.Kind = tkInteger) then
begin
SetOrdProp(AObject,PropInfo,AValue);
Result:=True;
end else
Result:=False;
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;
var
i : Integer;
s : string;
sFileName : string;
begin
if AFileName = '''' then
sFileName := FsLanguage //FsLanguage为默认语言文件名
else
sFileName := AFileName;
if not FileExists(sFileName) then Exit;
with TIniFile.Create(sFileName) do
try
//下面为读取变量
SAppName := ReadString(SIniLanguage,''AppName'',S_AppName);
SMenu := ReadString(SIniLanguage,''Menu'',S_Menu);
SNull := ReadString(SIniLanguage,''Null'',S_Null);
SAbout := ReadString(