Solved

Changing colors of buttons

Posted on 2000-02-13
10
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
can't see any reason for that - why not to change Button properies at runtime ?
;-//

0
 

Expert Comment

by:dethstar
Comment Utility
Alisher:

Hmm .... sounds good. How?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:Alisher_N
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

14 Experts available now in Live!

Get 1:1 Help Now