Home Articles Books Downloads FAQs Tips

Q: Change the text color on a push button


Answer:

A standard windows push button does not allow you to easily change the color of its text. This explains why changing the font properties of a standard TButton doesn't have any impact. In order to change the text color of a button, you must either subclass the control or owner draw the control. Fortunately, C++Builder provides a simple workaround. The BitBtn control already owner draws itself. In fact, it owner draws itself even if you don't display a glyph on the button.

To change the color of the text on a button, simply replace it with a BitBtn component. Set the text color using the Font property of the button.

Note: You can prove that TBitBtn is owner drawing itself by looking at the CreateParams member function. It looks like this (converted to C++):

void __fastcall TBitBtn::CreateParams(TCreateParams &Params)
{
    TButton::CreateParams(Params);
    Params.Style |= BS_OWNERDRAW;
}

Note: After proving that TBitBtn is using old-fashioned owner drawing, you can then see for yourself how TBitBtn uses the Font property. Look inside the TBitBtn::DrawItem function. You should see something like this inside the function (once again, I converted the code to C++ and added comments):

// FCanvas is the canvas that the button is painted with.
// this->Font is the Font property of the control
FCanvas->Font = this->Font;

// after setting the font, a function is called the draws the glyph,
// the text, and the background of the button using FCanvas


Copyright © 1997-2000 by Harold Howe.
All rights reserved.