Solved

Changing colors of buttons

Posted on 2000-02-13
10
221 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 108
When i run adoquery my application freezes 26 166
how to update exe applicatio from internet ? 6 78
Firemonkey DbLookupComboBox equivalent ? 2 48
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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