Solved

Changing colors of buttons

Posted on 2000-02-13
10
220 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi with SQL Natvie Client 15 86
delphi custom sort exception 6 137
Reconfigure Delphi Install? 2 46
Tviruailstringtree sort multi columns on header click 1 54
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now