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:
1 2 3 |
Memo1->StyleLookup = "Memostyle"; |
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:
1 2 3 4 |
Memo1->StyleLookup = "Memostyle"; auto fmxobj = Edit1->FindStyleResource("background", false); |
If this resource object we can create a rectangle (TRectangle
) as a background:
1 2 3 |
std::unique_ptr<TRectangle> rect(new TRectangle(fmxobj)); |
Here we used unique_ptr
which more modern to create this rectangle. Now we can set properties of our Rectangle, including its color.
1 2 3 4 5 6 7 |
rect->Align = TAlignLayout::Client; rect->Fill->Color = color; rect->Stroke->Color = color; // = claNull; rect->HitTest = false; rect->SendToBack(); |
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.
1 2 3 4 |
fmxobj->AddObject(rect.get()); rect.release(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- bool ChangeMemoColor(TMemo *memo, TAlphaColor color) { memo->StyleLookup = "Memostyle"; auto fmxobj = memo->FindStyleResource("background", false); if(fmxobj!=NULL) { std::unique_ptr<TRectangle> rect(new TRectangle(fmxobj)); rect->Align = TAlignLayout::Client; rect->Fill->Color = color; rect->Stroke->Color = color; // = claNull; rect->HitTest = false; rect->SendToBack(); fmxobj->AddObject(rect.get()); rect.release(); return true; } else return false; } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { ChangeMemoColor(Memo1, claYellow); ChangeMemoColor(Memo2, claDeepskyblue); ChangeMemoColor(Memo3, claGreenyellow); } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition