ibrary(Hinst);
end;
如果你的程序想在国际上供使用不同语言的人使用的话,用Dll来存放字符资源将是一个好方法.因为Dll不象ini文件那样可以被人随便修改,特别是有时侯如果想保存一些版权信息的话用Dll就再好不过了。比如说你准备开发一个"汉字简繁体翻译器"软件,准备提供Gb32,Big5码和英文三种语言菜单给用户,那么你可以试试用Dll来保存字符资源.
我们需要建立三个Dll.第一步当然是写Rc文件,举Gb32码为例,内容如下:
/*MySc.rc*/
#define IDS_MainForm_Caption 1
#define IDS_BtnOpen_Caption 2
#define IDS_BtnSave_Caption 3
#define IDS_BtnBig5_Caption 4
#define IDS_BtnGb32_Caption 5
#define IDS_BtnHelp_Caption 6
#define IDS_Help_Shelp 7
Stringtable
{
IDS_MainForm_Caption,"汉字简繁体翻译器"
IDS_BtnOpen_Caption,"打开文件"
IDS_BtnSave_Caption,"保存文件"
IDS_BtnBig5_Caption,"转换成Big5"
IDS_BtnGb32_Caption,"转换成Gb32"
IDS_BtnHelp_Caption,"帮助"
IDS_Help_Shelp,"输入文字或打开文件后按需要点击按钮即可转换!"
}
第二步是Brcc32编译为Res文件后用上面的方法得到Dll文件,另外两个Dll用同样的方法生成.下面来应用一下:
新建一个工程,放上五个Button:BtnOpen,BtnSave,BtnBig5,BtnGb32和BtnHelp.还有一个TComboBox:CbSelect用来选择语言种类的.
具体代码如下:
unit Unit1;
interface
private
SHelp: string;
function SearchLanguagePack: TStrings;
procedure SetActiveLanguage(LanguageName: string);
{ Private declarations }
implementation
procedure TForm1.CbSelectChange(Sender: TObject);
begin
SetActiveLanguage(CbSelect.Text);//调用相应Dll文件读取相应字符.
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CbSelect.Items.AddStrings(SearchLanguagePack);//搜索当前目录下所有的Dll文件名称
end;
function TForm1.SearchLanguagePack: TStrings;
var
ResultStrings: TStrings;
DosError: integer;
SearchRec: TsearchRec;
begin
ResultStrings := TStringList.Create;
DosError := FindFirst(ExtractFilePath(ParamStr(0)) + ''*.dll'', faAnyFile, SearchRec);
while DosError = 0 do
begin
ResultStrings.Add(ChangeFileExt(SearchRec.Name, ''''));
DosError := FindNext(SearchRec);
end;
FindClose(SearchRec);
Result := ResultStrings;
end;
procedure TForm1.SetActiveLanguage(LanguageName: string);
var
Hdll: Hmodule;
MyChar: array[0..254] of char;
DllFileName: string;
begin
DllFileName := ExtractFilePath(ParamStr(0)) + LanguageName + ''.dll'';
if not FileExists(DllFileName) then Exit;
Hdll := loadlibrary(Pchar(DllFileName));
Loadstring(hdll, 1, MyChar, 254);
Self.Caption := MyChar;
//读取字符资源,1表示资源文件中定义的1
Loadstring(hdll, 1, MyChar, 254);
Self.Caption := MyChar;
Loadstring(hdll, 2, MyChar, 254);
BtnOpen.Caption := MyChar;
Loadstring(hdll, 3, MyChar, 254);
BtnSave.Caption := MyChar;
Loadstring(hdll, 4, MyChar, 254);
BtnBig5.Caption := MyChar;
Loadstring(hdll, 5, MyChar, 254);
BtnGb32.Caption := MyChar;
Loadstring(hdll, 6, MyChar, 254);
BtnHelp.Caption := MyChar;
Loadstring(hdll, 7, MyChar, 254);
SHelp := MyChar;
Freelibrary(hdll);
Application.Title := Self.Caption;
//------------------------
BtnOpe