Link to home
Start Free TrialLog in
Avatar of skanade
skanade

asked on

Changing colors of buttons

I want to change the color of some buttons by handling the paint events in the form. Is there a sample code that I can use? I don't want to use bitmaps on buttons.
Avatar of dethstar
dethstar

Why dont you want to use bitmaps?

I dont know how to "capture" the paint method of a control, but if you're looking for an alternative then let me know and I'll show you how to do custom button shapes using bitbuttons and a few WinAPI calls.

If I figure anything out about paint methods - I'll let you know. There must be a way to do it as I believe Delphi is quite flexable.
ASKER CERTIFIED SOLUTION
Avatar of dethstar
dethstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another thought.

Taking the above example and creating a custom component and overloading the paint method would create a nice custom colored button dont ya think?
can't see any reason for that - why not to change Button properies at runtime ?
;-//

Alisher:

Hmm .... sounds good. How?
TWinControl.Brush
Determines the color and pattern used for painting the background of the control.

property Brush: TBrush;

Description

Read the Brush property to determine the style and color of the control’s background. Brush is a read-only property, because a windowed control owns a single Brush object to handle the painting of its background. The attributes of the brush can be changed, however, by using the Color and Style properties of the TBrush object. Further, all of the attributes of the brush can be changed to match those of another brush, by using the Assign method of the TBrush object.

(?)
Thats all great. It was the first thing I tried and it didnt work but on closer inspection of the TButton class I noticed that Delphi buttons are implemented using WinAPI with the default push button style (meaning they will always be windows boring gray) and not drawn by Delphi itself (as far as i can find).I looked at WinAPI buttons a bit closer and found that they do contain a user-drawn property instead of the default push-button style that Delphi uses, but I havent quite figured out how to implement it yet.
Thats all great. It was the first thing I tried and it didnt work but on closer inspection of the TButton class I noticed that Delphi buttons are implemented using WinAPI with the default push button style (meaning they will always be windows boring gray) and not drawn by Delphi itself (as far as i can find).I looked at WinAPI buttons a bit closer and found that they do contain a user-drawn property instead of the default push-button style that Delphi uses, but I havent quite figured out how to implement it yet.
 There are the dozens of "Super cool color button" components in the net, including ones with full sources. Why should you need to re-create the bike ? ;-) Just look at www.torry.ru and you'll find all that you wand and much,much more...
Avatar of skanade

ASKER

Sorry, I couldn't get back here due to many reasons.

I don't want to use components as far as possible (mainly administrative and source control problems).

Anyway, for two of my uses, I found two different solutions which seem acceptable:

1) I used a TBitBtn with a bitmap (I didn't want to do that originally but in absence of an easier solution, I had to do it)

2) For other toggle type color buttons, I simulated them with TPanel which has easy color properties.

In any case, I am awarding the points to the answer with the most effort (code sample).

Thanks.