2

First of all I have to say that I've read this question of SO. But actually it wasn't helpful for me.

I want to add icons to TTabControl but it doesn't seems as easy as I can do it in VCL (TPageControl). As you know there is no something like Image Index in TTabControl.

So what's the easiest way to do this?

Thanks for your help.

Community
  • 1
  • 1
Sky
  • 4,244
  • 7
  • 54
  • 83

1 Answers1

3

I would suggest not going down the route of modifying the style given the inherent 'copy and paste "inheritance"' nature of the exercise, which becomes an issue if you're targeting more than one OS (even just Windows 7 and Windows 8.x). Instead, try this:

1) For each item you want an icon on, change its TextAlign property to taTrailing and pad its Text with four leading space characters.

2) Add one TImage to the form per tab, and load small bitmaps into them as desired.

3) Associate each tab item with its image by (for example) assigning its TagObject property to the image control in an OnCreate handler for the form:

procedure TForm1.FormCreate(Sender: TObject);
begin
  TabItem1.TagObject := Image1;
  //...
end;

4) Assign each tab item's OnPaint event the following shared event handler:

procedure TForm1.TabItemPaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
var
  B: TBitmap;
  SrcR, DstR: TRectF;
  TabItem: TTabItem;
begin
  TabItem := (Sender as TTabItem);
  B := (TabItem.TagObject as TImage).Bitmap;
  SrcR := RectF(0, 0, B.Width, B.Height);
  DstR := SrcR;
  DstR.Fit(RectF(ARect.Left, ARect.Top, ARect.Left + ARect.Height, ARect.Bottom));
  if not TabItem.IsSelected then DstR.Offset(0, 1);
  Canvas.DrawBitmap(B, SrcR, DstR, 1);
end;
Chris Rolliston
  • 4,788
  • 1
  • 16
  • 20