Solved

Changing colors of buttons

Posted on 2000-02-13
10
225 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:skanade
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Expert Comment

by:dethstar
ID: 2517734
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.
0
 

Accepted Solution

by:
dethstar earned 50 total points
ID: 2517789
A long way round would be to use an image and paint the canvas manually.

Something Like this:
A form with an image on it.

procedure TForm1.FormCreate(Sender: TObject);
var
  t : Array[1..5] of TPoint;
begin
 t[1].x := 0;
 t[1].y := 0;
 t[2].x := Image1.Width-1;
 t[2].y := 0;
 t[3].x := Image1.Width-1;
 t[3].y := Image1.Height-1;
 t[4].x := 0;
 t[4].y := Image1.Height-1;
 t[5].x := 0;
 t[5].y := 0;

 With Image1.Canvas Do
 Begin
   Pen.Color := clBlack;
   PolyLine(t);
   Brush.Color := clAqua;
   Brush.Style := bsSolid;
   Floodfill(2,2,clBlack,fsBorder);
   Pen.Color := clWhite;
   MoveTo(Image1.Width-2,1);
   LineTo(1,1);
   LineTo(1,Image1.height-2);
   Pen.Color := clGray;
   LineTo(Image1.Width-2,Image1.Height-2);
   LineTo(Image1.Width-2,1);
 End;
End;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  t : Array[1..5] of TPoint;
begin
 t[1].x := 0;
 t[1].y := 0;
 t[2].x := Image1.Width-1;
 t[2].y := 0;
 t[3].x := Image1.Width-1;
 t[3].y := Image1.Height-1;
 t[4].x := 0;
 t[4].y := Image1.Height-1;
 t[5].x := 0;
 t[5].y := 0;

 With Image1.Canvas Do
 Begin
   Pen.Color := clBlack;
   PolyLine(t);
   Brush.Color := clAqua;
   Brush.Style := bsSolid;
   Floodfill(2,2,clBlack,fsBorder);
   Pen.Color := clGray;
   MoveTo(Image1.Width-2,1);
   LineTo(1,1);
   LineTo(1,Image1.height-1);
   Pen.Color := clWhite;
   LineTo(Image1.Width-1,Image1.Height-1);
   LineTo(Image1.Width-1,1);
 End;

end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  t : Array[1..5] of TPoint;
begin
 t[1].x := 0;
 t[1].y := 0;
 t[2].x := Image1.Width-1;
 t[2].y := 0;
 t[3].x := Image1.Width-1;
 t[3].y := Image1.Height-1;
 t[4].x := 0;
 t[4].y := Image1.Height-1;
 t[5].x := 0;
 t[5].y := 0;

 With Image1.Canvas Do
 Begin
   Pen.Color := clBlack;
   PolyLine(t);
   Brush.Color := clAqua;
   Brush.Style := bsSolid;
   Floodfill(2,2,clBlack,fsBorder);
   Pen.Color := clWhite;
   MoveTo(Image1.Width-2,1);
   LineTo(1,1);
   LineTo(1,Image1.height-2);
   Pen.Color := clGray;
   LineTo(Image1.Width-2,Image1.Height-2);
   LineTo(Image1.Width-2,1);
 End;

end;

Doing text on the button could be as easy as placing a label on it.

This way is long and sloppy - i know, but Im still working on it.


0
 

Expert Comment

by:dethstar
ID: 2517796
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Alisher_N
ID: 2518110
can't see any reason for that - why not to change Button properies at runtime ?
;-//

0
 

Expert Comment

by:dethstar
ID: 2519549
Alisher:

Hmm .... sounds good. How?
0
 
LVL 3

Expert Comment

by:Alisher_N
ID: 2520490
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.

(?)
0
 

Expert Comment

by:dethstar
ID: 2521975
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.
0
 

Expert Comment

by:dethstar
ID: 2521997
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.
0
 

Expert Comment

by:kser
ID: 2527161
 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...
0
 

Author Comment

by:skanade
ID: 2582896
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question