C++ Builder is the easiest and fastest C++ IDE for building professional applications with powerful GUI components like Memo, Edit, ListBox, StringGrid and many more. Each component can be skinned with Styles to change their visual appearance. Styles are very powerful and because of this it can sometimes take a little to get used to how they work. In this post, we explain how to change background color of a Memo (TMemo
), one of the most frequently asked questions about using styles. These methods below, can be applied to other components too.
Table of Contents
How to change the background color of a Memo in C++ Builder by using Styles?
Styles are sets of graphical details that define the look and feel of an application. They are one of most beautiful and useful UI features of RAD Studio that can really add some extra professionalism to your apps. 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 Styles in C++ Builder, RAD Studio. More details about Styles can be found here. You can also find Premium Styles here. If you want to learn how you can modernize your components with styles this post has some great details;
What are the steps to change the background color of a Memo in C++ Builder by using Styles?
1. Right-click on the Memo and select 'Edit Custom Style...'
2. Expand Memo1Style
: 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
as below
4. Click on the Apply Style
button in the 'Style Designer'
, Save All.
5. Compile and run your application
If you still have problems setting the color, this DocWiki article can help you: https://blogs.embarcadero.com/edit-custom-style-to-change-the-background-color-of-a-fmx-tedit/
How to change the background color of a Memo in C++ Builder code?
If you want to change the background color of a Memo component (TMemo
) in C++ Builder, first you should set the StyleLookup
to “Memostyle
” as shown below:
If you look at the Custom Style of Memo,
There is a “background” property, so we should find this resource by using FindStyleResource()
method of Memo as given in the example below:
If this resource object we can create a rectangle (TRectangle
) as a background:
Here we used unique_ptr
which more modern to create this rectangle. Now we can set properties of our Rectangle, including its color.
Now, at last we need to add this object by using the .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.
Is there a full example of how to change the background color of a Memo in C++ Builder code?
Here is a full C++ Builder FMX example that changes the color of 3 TMemo
components that has Memo1
, Memo2
, Memo3
names. To do this we create a ChangeMemoColor()
function that uses the Memo name and your color. Don’t forget to drag them into your application form, before you run this code.
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.