学院首页>网络编程>其它编程>Delphi设计可中/英文切换的界面技巧

Delphi设计可中/英文切换的界面技巧

作者: 来源:Internet 添加时间:2006-5-25 21:27:46
  首先新建一工程,然后在窗体FORM1中加入一些控件,在这里我假设加入了如下控件:三个TBUTTON按钮,两个TCHECKBOX,一个TGROUPBOX和一个菜单。

  然后把他们的CAPTION属性改为中文信息,再将对应的英文信息放在这些控件的HINT属性中,信息如下:

控件名称CAPTION属性值HINT属性值
Button1中文Chinese
Button2英文English
Button3忽略Ignore
CheckBox1一般General
CheckBox2高级 Advanced
GroupBox1 信息 Information
PopupMenu1   
N1当前日期Current Date
N2 帮助 Help
N3 关于About
N4 退出 Exit

  最后将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环境下调试通过)



站内搜索