Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing colors of buttons

Posted on 2000-02-13
10
Medium Priority
?
228 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 150 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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