博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用INI文件实现界面无闪烁多语言切换
阅读量:4515 次
发布时间:2019-06-08

本文共 4637 字,大约阅读时间需要 15 分钟。

{  For example :

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  SetActiveLanguage(ComboBox1.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);

begin
  ComboBox1.Items.AddStrings(SearchLanguagePack);
end;
}

unit uLanguage;

interface

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Menus, IniFiles, ComCtrls
  ,AdvGlowButton, AdvNavBar, AdvOfficePager
  ;
//搜索ini文件生成语言列表
function SearchLanguagePack: TStrings;
//根据当前选择的语言设置指定窗体及其子控件的属性
procedure SetActiveLanguage(owner: TForm; const LanguageName: string);
//将指定窗体及其子控件的属性写入INI文件
procedure Writeproperty(owner: TForm; const LanguageName: string);

implementation

function SearchLanguagePack:TStrings;

var
  ResultStrings:TStrings;
  DosError:integer;
  SearchRec:TsearchRec;
begin
  ResultStrings:=TStringList.Create;
  DosError:=FindFirst(ExtractFilePath(Application.ExeName)+'language/'+'*.ini', faAnyFile, SearchRec);
  while DosError=0 do
  begin
    { 返回的文件名并去掉末尾的.ini字符 }
    ResultStrings.Add(ChangeFileExt(SearchRec.Name,''));
    DosError:=FindNext(SearchRec);
  end;
  FindClose(SearchRec);
  Result:=ResultStrings;
end;

procedure Writeproperty(owner: TForm; const LanguageName: string);

var
  c:TComponent;
  i:Integer;
begin
  if owner = nil then exit;
  with TInifile.Create(ExtractFilePath(Application.ExeName)+'language/'+LanguageName+'.ini') do
  begin
    WriteString(owner.Name, owner.Name+'.Caption', owner.Caption);
    for i:=0 to owner.ComponentCount-1 do
    begin
      c:=owner.Components[i];
      if c is TLabel then
      begin
        writeString(owner.Name, c.Name+'.Caption', (c as TLabel).Caption);
      end else
      if c is TCheckBox then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TCheckBox).Caption);
      end else
      if c is TButton then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TButton).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TButton).Hint);
      end else
      if c is TMenuItem then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TMenuItem).Caption);
      end else
      if c is TToolButton then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TToolButton).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TToolButton).Hint);
      end else
      if c is TAdvGlowButton then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TAdvGlowButton).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TAdvGlowButton).Hint);
      end else
      if c is TAdvOfficePage then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TAdvOfficePage).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TAdvOfficePage).Hint);
      end else
      if c is TAdvNavBarPanel then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TAdvNavBarPanel).Caption);
      end;
    end;
    //处理字符串变量
  //  M1:=ReadString(Messages,'M1',M1);
  end;
end;

procedure SetActiveLanguage(owner: TForm; const LanguageName:string);

const
  Messages='Messages';    //字符串变量小节
var
  c:TComponent;
  i:Integer;
begin
  if owner = nil then exit;
  with TInifile.Create(ExtractFilePath(Application.ExeName)+'language/'+LanguageName+'.ini') do
  begin
     owner.Caption := readstring(owner.Name, owner.Name+'.Caption', owner.Caption);
    for i:=0 to owner.ComponentCount-1 do
    begin
      c:=owner.Components[i];
      if c is TLabel then
      begin
        (c as TLabel).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TLabel).Caption);
      end else
      if c is TCheckBox then
      begin
        (c as TCheckBox).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TCheckBox).Caption);
      end else
      if c is TButton then
      begin
        (c as TButton).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TButton).Caption);
        (c as TButton).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TButton).Hint);
      end else
      if c is TMenuItem then
      begin
        (c as TMenuItem).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TMenuItem).Caption);
      end else
      if c is TToolButton then
      begin
        (c as TToolButton).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TToolButton).Caption);
        (c as TToolButton).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TToolButton).Hint);
      end else
      if c is TAdvGlowButton then
      begin
        (c as TAdvGlowButton).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TAdvGlowButton).Caption);
        (c as TAdvGlowButton).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TAdvGlowButton).Hint);
      end else
      if c is TAdvOfficePage then
      begin
        (c as TAdvOfficePage).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TAdvOfficePage).Caption);
        (c as TAdvOfficePage).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TAdvOfficePage).Hint);
      end else
      if c is TAdvNavBarPanel then
      begin
        (c as TAdvNavBarPanel).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TAdvNavBarPanel).Caption);
      end;
    end;
    //处理字符串变量
  //  M1:=ReadString(Messages,'M1',M1);
  end;
end;

end.

转载于:https://www.cnblogs.com/hnxxcxg/archive/2011/01/02/2940938.html

你可能感兴趣的文章
Android 适配知识点
查看>>
Android中常用的几种加密
查看>>
50.Android编码规范
查看>>
linux下source命令的基本功能
查看>>
linux-IO重定向-文本流重定向
查看>>
MSSQLSERVER服务无法启动的解决方案
查看>>
MySQL数据库管理
查看>>
ASP.NET中进度条的简单应用
查看>>
Java Activiti6.0 spring5 SSM 工作流引擎 审批流程 java项目框架
查看>>
md5
查看>>
Linux下的crontab定时执行任务命令详解
查看>>
C#高级编程(第7版) Professional C# 4 and .NET 4 - 读书笔记
查看>>
ipad4自动下载了ios8的安装包,好几个G啊,不想更新,怎么删了呢?
查看>>
JS中的Navigator 对象
查看>>
Android 开源控件与常用开发框架开发工具类
查看>>
记录一次网站打开卡--排故障过程
查看>>
第四章小结
查看>>
Windows7下python2.7.6环境变量配置
查看>>
java设计模式------代理模式
查看>>
WPF学习笔记----注释标记,使用自定义资源字典(style)文件的流程
查看>>