Reputation: 99
I'm using onPaint event handler on the custom button, but i don't know what to write to source an image list or add a caption to this button.
procedure TForm1.TitleBarPanel1CustomButtons0Paint(Sender: TObject);
begin
...
end;
Upvotes: 2
Views: 1278
Reputation: 389
In order to have an icon from a TImageList:
procedure TfFPer.TitleBarPanel1CustomButtons0Paint(Sender: TObject);
begin
const c=(Sender as TSystemTitlebarButton).Canvas;
const xPos=1;
const yPos=1;
const imageIndexInTImageList=10;
CsColor.i.Draw(c,xPos,yPos,imageIndexInTImageList);
end;
In order to have it centered:
procedure TfFPer.TitleBarPanel1CustomButtons0Paint(Sender: TObject);
begin
const c=(Sender as TSystemTitlebarButton).Canvas;
const w=CsColor.i.Width;
const h=CsColor.i.Height;
var xPos := (c.ClipRect.Width-w) div 2;
var yPos := (c.ClipRect.Height-h) div 2;
const imageIndexInTImageList=10;
CsColor.i.Draw(c,xPos,yPos,imageIndexInTImageList); // CsColor.i is my TImageList
end;
But when running it, I found a strange behaviour: the custom button canvas ClipRect changes it sizes! At the very first run, it is 0,0,183,30. So it is not visible. When mouse hover, it is 0,0,46,30, and now it os ok. After click, it disappear again. I'm looking for a fix.
Upvotes: 0
Reputation: 12312
In this event handler, Sender
is actually a TSystemTitlebarButton
. You can cast it to access his properties such as the Canvas
. Having the Canvas
, you can draw whatever is needed.
Simple use example:
procedure TForm1.TitleBarPanel1CustomButtons0Paint(Sender: TObject);
begin
(Sender as TSystemTitlebarButton).Canvas.Rectangle(0, 0, 10, 10);
end;
Upvotes: 4