C++ 构建器应用程序设置?

标签 c++ xml c++builder ini

我想知道如何在 c++ builder 中保存 xml 或 ini 文件中的应用程序设置。知道 visual studio 在“设置”中具有这些功能,我正在 c++ builder 中搜索相同的功能。

用什么方法解决。

最佳答案

当使用 C++ Builder 创建 VCL 应用程序时,您可以使用 ini 文件来保存您希望在下次启动应用程序时恢复的设置和值。

首先,包含 IniFiles.hpp header 。

#include <IniFiles.hpp>

要保存设置和值,创建一个新的 TIniFile 并在 OnClose 事件中写入它。

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    bool booleanValueToSave = true;
    int integerValueToSave = 42;

    TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));

    ini->WriteString("SectionName", "KeyName", "Value to save as KeyName");
    ini->WriteBool("SectionName", "AnotherKeyName", booleanValueToSave);
    ini->WriteInteger("SectionName", "YetAnotherKeyName", integerValueToSave);

    // To save something like the window size and position
    ini->WriteInteger("Settings", "WindowState", Form1->WindowState);
    if (Form1->WindowState == wsNormal)
    {
        ini->WriteInteger("Settings", "MainFrm Top", Form1->Top);
        ini->WriteInteger("Settings", "MainFrm Left", Form1->Left);
        ini->WriteInteger("Settings", "MainFrm Height", Form1->Height);
        ini->WriteInteger("Settings", "MainFrm Width", Form1->Width);
    }

    delete ini;
}

不要忘记删除!!

那里的代码将创建一个与您的可执行文件同名但扩展名为 .ini 的 ini 文件。将有两个标题,“SectionName”和“Settings”。在标题下,您将看到键值对,例如“AnotherKeyName=true”和“YetAnotherKeyName=42”。

然后,要在您的应用程序启动时恢复这些值,请创建一个新的 TIniFile 并在 OnCreate 事件中从中读取。

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    TWindowState ws;
    int integerValueToRestore;
    bool booleanValueToRestore;
    int someDefaultIntegerValueIfTheKeyDoesntExist = 7;
    bool someDefaultBooleanValueIfTheKeyDoesntExist = false;

    TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));

    integerValueToRestore = ini->ReadInteger("SectionName", "YetAnotherKeyName", someDefaultIntegerValueIfTheKeyDoesntExist);
    booleanValueToRestore = ini->ReadBool("SectionName", "AnotherKeyName", someDefaultBooleanValueIfTheKeyDoesntExist);

    // To restore the window size and position you saved on FormClose
    ws = (TWindowState)ini->ReadInteger("Settings", "WindowState", wsNormal);
    if (ws == wsMinimized)
        ws = wsNormal;
    if (ws == wsNormal)
    {
        Form1->Top = ini->ReadInteger("Settings", "MainFrm Top", 10);
        Form1->Left = ini->ReadInteger("Settings", "MainFrm Left", 10);
        Form1->Height = ini->ReadInteger("Settings", "MainFrm Height", 730);
        Form1->Width = ini->ReadInteger("Settings", "MainFrm Width", 1028);
    }

    Form1->WindowState = ws;

    delete ini;
}

希望对您有所帮助。

关于C++ 构建器应用程序设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146388/

相关文章:

c++ - 使用 cin 对象进行输入验证并清除缓冲区 (c++)

android - 按钮边距随按钮文本内容而变化

java - Web XML 导致应用程序崩溃?

c++ - 如何使用Windows api中的参数执行线程?

c++ - 通过 MPI 在集群中进行主成分分析

c++ - 我可以将 vector of vector 的第一个元素的地址直接传递给需要 unsigned char 作为其输入的函数吗?

c# - 如何防止 XmlReader 跳到文件末尾,或者如何重置阅读器

c++builder - VCL多重继承

qt - 将 Borland C++ Builder 移植到 Qt

c++ - 检查 boost 属性树中的值是树还是终端值