Site icon Learn C++

How To Change Background Color Of An Edit In An FMX C++ App

How To Change Background Color Of An Edit In An FMX C++ App

C++ Builder is the easiest and fastest C++ IDE for building everything from simple to professional applications using visually amazing GUI controls and forms. Each form and component can be skinned with Styles which allow your application to have its own professionally designed attractive look and feel. The Style systems of the VCL and FMX (FireMonkey) award-winning frameworks is very easy to use but there is sometimes a learning curve to climb to understand how to get the best out of them. In this post, we explain how to change background color of an Edit (TEdit) which is one of the most frequently asked questions. These methods below, can be applied to other components too.

How to change the background color of an Edit control using Styles?

Styles are sets of graphical details that define the look and feel of an application visually. They are one of most beautiful and useful UI features of RAD Studio, that makes your UI elements ‘skinned’ with a professionally designed graphical look and feel. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps (not wrapped in some kind of runtime interpretation layer) and the powerful FireMonkey (FMX) framework for cross-platform UIs. Both VCL and FMX apps support Styles.

There are many different styles available, and they cater for almost every possible taste in aesthetics as well as providing dark and light mode versions. More details about Styles can be found here. You can also find additional Premium Styles here. If you want to learn how you can modernize your components with styles here is the post you need;

  1. Right-click on the Edit and select 'Edit Custom Style...'
  2. Expand Edit1Style: you’ll see the background node. Click on background to select it. Then add a TRectangle via the Palette. The IDE should expand the background node and show a new Rectangle1Style tied to the TRectangle,
  3. Via the Object Inspector change the Color of TRectangle
  4. Click on the Apply Style button in the 'Style Designer'.
  5. Compile and run your application

If you still have problems to set color, may be this docwiki can help you : https://blogs.embarcadero.com/edit-custom-style-to-change-the-background-color-of-a-fmx-tedit/

How to change background color of an Edit in C++ code?

If you want to change the background color of a Edit component (TEdit) in C++ Builder, first you should set the StyleLookup property to “Editstyle” as below.

[crayon-663a23b088b60459295383/]

If you look at the Custom Style of Edit,

there is “background” property, so we should find this resource by using the FindStyleResource() method of Edit as given example below.

[crayon-663a23b088b68371583649/]

if this resource object found, we can create a rectangle (TRectangle) as a background as below.

[crayon-663a23b088b6b358177009/]

Here we used unique_ptr which more modern to create this rectangle. Now we can set properties of our Rectangle, including its color.

[crayon-663a23b088b6c445126174/]

Now, at last we need to add this object by using .get() method of unique_ptr. And the final trick here is you must release this unique_ptr by using .release() otherwise it doesn’t have any effect.

[crayon-663a23b088b6e138106364/]

I asked one of Embarcadero’s engineers to explain this a little more to me and he noted that “If you insist on using unique_ptr, you must add a call to .release(); to tell the unique_ptr that you no longer want it to clean up the pointer; otherwise, it will not work“.

Let’s sum up all in a full example.

Is there a full example of how to change the background color of an Edit in C++ code?

Here is a full C++ Builder FMX example that changes the color of 3 TEdit components with the names Edit1, Edit2, and Edit3. To do this we create a EditColor() function that uses Edit name and your color. Don’t forget to drag them into your application form, before you run this code.

[crayon-663a23b088b70401345576/]

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Exit mobile version