TColorButton With Color Properties

Make Your Own Button Component With Custom Colors

colors on a painter's palette

Topic Images, Inc./Topic Images/Getty Images

The background color of a TButton is controlled by Windows, not Delphi. TButton is a simple wrapper around the standard Windows button, and Windows does not allow it to be colored except by choosing the colors in Control Panel. 

This means you can not set the background color of a TButton, nor you can change the background color of TBitBtn or TSpeedButton.

Since Windows insists on doing the background coloring with clBtnFace, the only way to change it is to draw the button yourself by making an owner-drawn button component.

TColorButton Source Code

The TColorButton adds three new properties to the standard TButton:

  • BackColor - Specifies the background color of the button
  • ForeColor - Specifies the color of the button text. Note that this "overrides" the Font.Color property
  • HoverColor - Specifies the color used to paint the button's background when the mouse hovers over the button.

Here's how to set color-related properties of the TColorButton at runtime:

ColorButton1.BackColor := clOlive; //background
ColorButton1.ForeColor := clYelow; //text
ColorButton1.HoverColor := clNavy; //mouse over

Installing Into a Component Palette

The TColorButton comes as a single unit file with the .PAS file extension. After downloading the component, you need to install the source component into an existing package.

mla apa chicago
Your Citation
Gajic, Zarko. "TColorButton With Color Properties." ThoughtCo, Jul. 31, 2021, Gajic, Zarko. (2021, July 31). TColorButton With Color Properties. Retrieved from Gajic, Zarko. "TColorButton With Color Properties." ThoughtCo. (accessed May 29, 2023).