Hi!
There is always the question for colored buttons. The answer is NO - with the standard components. There are very impressive buttons in BGRAcontrols and other libs, but sometime you want a simple and quick solution. That's what I did.
I "misstreat" the BitButton. A little.
I use the glyph inside the TBitbtn and paint the deserved color on it.
The I write the caption on the glyph.
I store the caption string in the tag property and set the caption to length=0.
If I would not do that, the Button will try to write the caption a second time - not nice.
So at design time
* set all properties like always
* color and font are respected
* set Margin and Spacing to Zero
* dont care ,about Layout - it is not used
All you have to do is to init the BitButton with this procedure:
procedure TForm1.Bitinit(B : TBitBtn);
var R : Trect;
cap: string;
dx,dy : integer;
P: Pstring;
begin
if B.Caption <> '' then
begin
cap := B.Caption;
B.Tag := PtrInt(@cap);
B.Caption := '';
end else // already done
begin
P := Pointer(B.Tag);
if B.Tag <> 0 then cap := P^ else cap := '???';
end;
B.glyph.SetSize(B.width,B.height);
R := Rect(0,0,B.width,B.height);
B.Glyph.Canvas.brush.Color := B.Color;
B.Glyph.Canvas.FillRect(R);
B.Glyph.Canvas.Font := B.Font;
dx :=(B.glyph.Width - B.Glyph.Canvas.TextWidth(cap)) div 2;
dy := (B.glyph.Height - B.Glyph.Canvas.TextHeight(cap)) div 2;
B.Glyph.Canvas.TextOut(dx,dy,cap);
end;
Then everything is fixed. As the glyph is a TBitmap you dont have to care about repainting - it is done automatic by the TBitmap.
If you have only on Button then use
If you have some more TBitBtn on your Form, you can do the init in a loop in FormCreate:
procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
for i := 0 to ComponentCount - 1 do
begin
if Components[i] is TBitBtn then
BitInit (TBitBtn(Components[i]));
end;// i
end;
Little image attached.
Have some fun with colored Buttons!
Winni