c++c++buildervclvcl-styles

How can I list all the available VCL styles in a combo box and in the ComboBoxChange event, apply that style in C++ Builder?


I'm currently trying to add a feature in my application where the user can select a VCL style to there preference. I could manually add all the styles into the ComboBox directly but i'm sure there's a much easier way.


Solution

  • Create a new C++Builder VCL application. In the Project | Options | Application | Appearance menu choose some Custom Style Names.

    Then add Button and ComboBox box components to your C++ VCL form. For the Button's onlick and ComboBox's Change events use the following code. You'll also need to put #include near the top of the source code for the form :D Compile and run, click the button to see the combobox populated with the styles you selected in the project option for appearance. Then select one of the styles from the combobox to change the style of the app.

    I've tested this code with RAD Studio 10.4 Sydney. Should work for any recent release of C++Builder.

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        ComboBox1->Items->BeginUpdate();
        try
        {
            ComboBox1->Items->Clear();
    
            DynamicArray<String> styleNames = Vcl::Themes::TStyleManager::StyleNames;
    
            for(int i = 0; i < styleNames.Length; ++i)
            {
                String styleName = styleNames[i];
                ComboBox1->Items->Add(styleName);
            }
        }
        __finally
        {
            ComboBox1->Items->EndUpdate();
        }
    }
    
    void __fastcall TForm1::ComboBox1Change(TObject *Sender)
    {
        // set the style for the selected combobox item
        Vcl::Themes::TStyleManager::TrySetStyle(ComboBox1->Items->Strings[ComboBox1->ItemIndex],false);
    }