Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ builder application settings?

I want to know way of how to save settings from application inside of xml or ini file inside c++ builder. Know that visual studio have those functionality inside "Settings", i'm searching for same functionality inside c++ builder.

What method to use to solve that.

like image 863
tonni Avatar asked Aug 09 '26 19:08

tonni


1 Answers

When using C++ Builder to create a VCL application, you can use an ini file to save settings and values you want to want to restore the next time your application is launched.

First, include the IniFiles.hpp header.

#include <IniFiles.hpp>

To save settings and values, create a new TIniFile and write to it in the OnClose event.

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

Don't forget the delete!!

That code there will create an ini file with the same name as your executable but with a .ini extension. There will be two headings, "SectionName" and "Settings." Under the headings you will see key-value pairs such as "AnotherKeyName=true" and "YetAnotherKeyName=42."

Then, to restore the values when you application is launched, create a new TIniFile and read from it in the OnCreate event.

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

Hope that helps.

like image 65
Ted Avatar answered Aug 11 '26 08:08

Ted



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!