Source Code for the TColorButton Delphi Component

Paint Tube, Palette
Topic Images, Inc./Topic Images/Getty Images


Have you ever asked yourself a question like: "How can I set the background color of a TButton?" Well, you can't! What's more you cannot change the background color a TButton, TBitBtn and TSpeedButton.

The background color of a TButton is controlled by Windows, not by 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 the Control Panel.


Note: to change the default button color system-wide, you need to open the Control Panel; in the Appearances tab of the Display properties applet edit the color of the "3D Objects" item (badly, this will change the color of some other Windows elements).

Since Windows insists on doing the background coloring with clBtnFace, the only way to change the background color is to draw the button yourself. In other words, if you want a button where you decide about the color, you will have to make an owner-drawn button component.


To overcome the lack of color-related properties of the standard TButton control, I've created the TColorButton custom Delphi component. 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 run time:

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

    Installing into a Component Palette

    First, download the component. The TColorButton comes as a single unit file (.pas extension). You'll need to add the component into an existing package. Here's "How to Install Custom Component in Delphi (into Existing Package)"