【问题标题】:C++ builder application settings?C++ builder应用程序设置?
【发布时间】:2013-02-15 06:49:47
【问题描述】:

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

用什么方法来解决。

【问题讨论】:

    标签: c++ xml c++builder ini


    【解决方案1】:

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

    首先,包含 IniFiles.hpp 标头。

    #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;
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多