在一些软件中,我们经常会看到界面语言切换功能,不过程序需要的这些各国语言信息都封装在DLL中,有的也存储在INI文件中,下面我就向大家介绍一个小技巧,在DELPHI中不需要任何DLL文件和INI文件,就可以实现此功能。
首先新建一工程,然后在窗体FORM1中加入一些控件,在这里我假设加入了如下控件:三个TBUTTON按钮,两个TCHECKBOX,一个TGROUPBOX和一个菜单。
然后把他们的CAPTION属性改为中文信息,再将对应的英文信息放在这些控件的HINT属性中,信息如下:
最后将Button1的TAG属性改为1,Button2的TAG属性改为2,双击FORM1,BUTTON1和BUTTON2,编写代码如下:
procedure TForm1.FormCreate(Sender :
Tobject);
begin
//初始化,显示中文界面
Button1.Enabled :=
False;
Button2.Enabled :=True
end;
procedure TForm1.ChangeState(Mode : Byte); //改变按钮状态
begin
if
Mode = 1 then
//如果是显示中文,则Button1失效,Button2有效
begin
Button1.Enabled :=
False;
Button2.Enabled :=
True;
End
Else
Begin
Button1.Enabled :=
True;
Button2.Enabled := False;
End;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
CS :
String;
Begin
ChangeState(Tbutton(Sender).Tag);
for i:=0
to ComponentCount-1 do
begin
//将窗体中的菜单项的中/英文进行切换
if
Components[i] is TMenuItem then
begin
CS :=
TMenuItem(Components[i]).Hint ;
TMenuItem(Components[i]).Hint:=
TMenuItem(Components[i]).Caption
;
TMenuItem(Components[i]).Caption := CS
;
end;
//将窗体中的按钮的中/英文进行切换
if Components[i] is TButton
then
begin
CS := TButton(Components[i]).Hint
;
TButton(Components[i]).Hint := TButton(Components[i]).Caption
;
TButton(Components[i]).Caption := CS
;
end;
//将窗体中的复选框的中/英文进行切换
if Components[i] is TCheckBox
then
begin
CS:=TCheckBox(Components[i]).Hint
;
TCheckBox(Components[i]).Hint:=TCheckBox(Components[i]).Caption
;
TCheckBox(Components[i]).Caption := CS
;
end;
//将窗体中的组合框的中/英文进行切换
if Components[i] is TGroupBox
then
begin
CS:=TGroupBox(Components[i]).Hint
;
TGroupBox(Components[i]).Hint:=TGroupBox(Components[i]).Caption
;
TGroupBox(Components[i]).Caption := CS
;
end;
end;
end;
最后再将Button2的ONCLICK事件指向Button1的ONCLICK事件,按F9,运行一下,看看效果,切换的速度也非常快,有兴趣的朋友可以试试。
(本程序在DELPHI6 WIN2000环境下调试通过)
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



