Adding Bitmaps to a Tabs

By using the TabCtrl_SetItem() meacro, adding Bitmaps to the Tabs of a PageControl or TabControl is easily accomplished.  This method is preferred over making the TabControl/PageControl owner-drawn, then drawing the items yourself. 

KEYWORDS: TabCtrl_SetItem
 



 
You'll need to have an ImageList with each item corresponding to a certain Tab, or set the iImage member of the TC_ITEM struct to assign the index of the Tab's image accordingly. 
 

//---------------------------------------------------------------------------

//The goal is to change the style of each TabSheet to 
//accomodate a bitmap (TCIF_IMAGE)... 
__fastcall TForm1::TForm1(TComponent* Owner) 
 : TForm(Owner) 

    //Get the current Style
    DWORD dwStyle = GetWindowLong(PageControl1->Handle, GWL_STYLE); 

    //Add the "put icon on left" style or TCS_FORCELABELLEFT 
    //depending on where you want the image
    SetWindowLong(PageControl1->Handle, GWL_STYLE,
                  dwStyle | TCS_FIXEDWIDTH | TCS_FORCEICONLEFT); 

    //Change the attributes of each tab
    TC_ITEM tci; 
    for (int index = 0; index < PageControl1->PageCount; index++) 
    { 
        //flag the image state 
        tci.mask = TCIF_TEXT | TCIF_IMAGE; 

        //add the previously assigned captions
        tci.pszText = PageControl1->Pages[index]->Caption.c_str(); 

        int max = PageControl1->Pages[index]->Caption.Length() * sizeof(char); 
        tci.cchTextMax = max;
        tci.iImage = index; 

        //force the changes
        TabCtrl_SetItem(PageControl1->Handle, index, &tci); 
    } 

    //associate an ImageList with the TabControl
    PageControl1->Perform(TCM_SETIMAGELIST, 0, (LPARAM)ImageList1->Handle);