1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

TColorButton - button with color properties
Full source code of the TColorButton Delphi component, an extension to the standard TButton control, with font color, background color and mouse over color properties.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Download FULL source
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Building Delphi components - tutorials
• Free source Delphi components
• How To "HotTrack" any control

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.

TColorButton properties 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.

TColorButton before hover TColorButton hover effect

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)"

Questions? Comments? Extensions? Exceptions?!
That's it. If you find this component practical and if you extend it by adding more properties, please send your source and make it available to other developers.
As always if there are any questions or comments please post them on the Delphi Programming Forum.

Related Video
Create a Color Sketch From a Photo in Photoshop Elements
Adobe Photoshop Web Button

©2016 About.com. All rights reserved.