procedure TForm1.LanguageEnglishClick(Sender: TObject);
const
MENU_TAG1='1' ; //测试菜单在ini中的编号
MENU_TAG2='2'; //设置的菜单在ini中的编号
MENU_TAG3='3'; // 帮助的
MENU_TAG4='4'; //语言的
var
Component: TComponent;
i,j:integer;
IniFileName:string;
MyIniFile:Tinifile; //ini文件名
begin
IniFilename:=ExtractFilePath(Paramstr(0))+'English.ini';
MyIniFile:=Tinifile.Create(IniFileName);
for j:=0 to Screen.Forms[0].ComponentCount - 1 do
begin
Component := Screen.Forms[0].Components[j] as TComponent;
if component is TmenuItem then
begin
if (TMenuItem(Component).Caption<>'') and (TMenuItem(Component).Caption <>'-') then
TMenuItem(Component).Caption := MyIniFile.ReadString('Menu','1'+ inttostr(j-1), TMenuItem(Component).Caption);
end;
end;
end;
const
MENU_TAG1='1' ; //测试菜单在ini中的编号
MENU_TAG2='2'; //设置的菜单在ini中的编号
MENU_TAG3='3'; // 帮助的
MENU_TAG4='4'; //语言的
var
Component: TComponent;
i,j:integer;
IniFileName:string;
MyIniFile:Tinifile; //ini文件名
begin
IniFilename:=ExtractFilePath(Paramstr(0))+'English.ini';
MyIniFile:=Tinifile.Create(IniFileName);
for j:=0 to Screen.Forms[0].ComponentCount - 1 do
begin
Component := Screen.Forms[0].Components[j] as TComponent;
if component is TmenuItem then
begin
if (TMenuItem(Component).Caption<>'') and (TMenuItem(Component).Caption <>'-') then
TMenuItem(Component).Caption := MyIniFile.ReadString('Menu','1'+ inttostr(j-1), TMenuItem(Component).Caption);
end;
end;
end;
但是好像一点反应也没有,不知道作在那里,请指教。
if component is TmenuItem then
改成
if component.ClassName= 'TmenuItem' then
行不行呢
试过了,好像不行:(
IniFilename:=ExtractFilePath(Paramstr(0))+'English.ini';
以上语句有错!
你可以写成这样:
IniFilename:=StringReplae(ExtractFilePath(Application.ExeName)+'\English.ini','\\','\',[rfReplaceAll]);你的文件名肯定没有取到呢!呵呵,我也犯过这样的错呢!
试过了,也不行啊!:(
这个方法是过了,没有用啊!:(
我你的问题可能是:
1。在下面的读取字符串时
TMenuItem(Component).Caption := MyIniFile.ReadString('Menu','1'+ inttostr(j-1), TMenuItem(Component).Caption);
'1'+ inttostr(j-1) 应该是 11,12,13,(你的意思是inttostr(j)?)
在ini的格式应该是
[menu]
11 = open
12 = close
不是
[menu]
1= open
2=closeMENU_TAG好像没有用?~~
试试 if Screen.Forms[0].Components[j].ClassNameis('TMENUITEM') then
这个应该是可以的。再不行就有可能是如 laihecongxi(兴哥) 所说的了
或者你单步调试一下if .. then语句后面的语句有没有被执行,把错误定位得更准确点
TMenuItem(Component).Caption := MyIniFile.ReadString('Menu','1'+ inttostr(j-1), TMenuItem(Component).Caption);
end;应该是这一行的问题。
错在那里那??
[menu]
10 = open
11 = close .........
所以我用j-1,menu_tag1是表示文件菜单,而menu_tag2是表示编辑菜单........
MyIniFile.ReadString('Menu','1'+ inttostr(j-1), TMenuItem(Component).Caption);
看有没有取出来
如果按你源代码,如果又其他组件的话j的值不是你想像的0,1,2...
所以下面这句要改一下。利用Tag值在判断一下
......
with TMenuItem(Component) do
Caption :=
MyIniFile.ReadString('Menu','1'+ inttostr(tag-1),Caption);{设置菜单item的Tag值为MENU_TAG,}
我想你的i是为循环Screen.form[i]预留的吧,测试过了全体窗体可以实现的。
thank u very much