如何在BCB中 对ini文件 进行读取 并把读取到的 显示在窗体上?
RT 希望 能基础点的方法, 详细点, 谢谢!
[解决办法]
CB中可以用TIniFile类来处理ini文件,下面是help文件中的例子
void __fastcall TForm2::btStoreClick(TObject *Sender)
{
/* Open an instance. */
TCustomIniFile* SettingsFile = OpenIniFileInstance();
// Store current form properties to be used in later sessions.
try
{
SettingsFile->WriteInteger (Name, "Top", Top);
SettingsFile->WriteInteger (Name, "Left", Left);
SettingsFile->WriteInteger (Name, "Width", Width);
SettingsFile->WriteInteger (Name, "Height", Height);
SettingsFile->WriteString (Name, "Caption", Caption);
SettingsFile->WriteBool (Name, "InitMax", WindowState == wsMaximized );
}
catch(Exception* e)
{
}
delete SettingsFile;
}
void __fastcall TForm2::btLoadClick(TObject *Sender)
{
/* Open an instance. */
TCustomIniFile* SettingsFile = OpenIniFileInstance();
try
{
/*
Read all saved values from the last session. The section name
is the name of the form. Also, use the form's properties as defaults.
*/
Top = SettingsFile->ReadInteger(Name, "Top", Top );
Left = SettingsFile->ReadInteger(Name, "Left", Left );
Width = SettingsFile->ReadInteger(Name, "Width", Width );
Height = SettingsFile->ReadInteger(Name, "Height", Height );
Caption = SettingsFile->ReadString (Name, "Caption", Caption);
// Load last window state.
if (SettingsFile->ReadBool(Name, "InitMax", WindowState == wsMaximized))
WindowState = wsMaximized;
else
WindowState = wsNormal;
}
catch(Exception* e)
{
}
delete SettingsFile;
}
TCustomIniFile* __fastcall TForm2::OpenIniFileInstance()
{
/*
Open/create a new INI file that has the same name as
your executable, only with the INI extension.
*/
switch (RadioGroup1->ItemIndex)
{
case 0:
/* Registry mode selected: in HKEY_CURRENT_USER\Software\... */
return new TRegistryIniFile(String("Software\") + Application->Title);
case 1:
/* Ini file mode selected */
return new TIniFile(ChangeFileExt(Application->ExeName, ".INI"));
case 2:
/* Memory based Ini file mode selected */
return new TMemIniFile(ChangeFileExt(Application->ExeName, ".INI"));
}
}
// 写操作
TIniFile* ini = NULL;
ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
// 写入 int
ini->WriteInteger("MainForm", "Left", Left);
ini->WriteInteger("MainForm", "Top", Top);
ini->WriteInteger("MainForm", "Height", Height);
ini->WriteInteger("MainForm", "Width", Width);
// 写入 str
ini->WriteString("Version","Ver", "1.0.0.1");
delete ini;
// 读操作
ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
int _left = ini->ReadInteger("MainForm", "Left", Left);
int _top = ini->ReadInteger("MainForm", "Top", Top);
int _height = ini->ReadInteger("MainForm", "Height", Height);
int _width = ini->ReadInteger("MainForm", "Width", Width);
String ver = ini->ReadString("Version", "Ver");
delete ini;