Adding Bitmaps to a ListView's Header Control 

 

Encapsulation of the ListView's Header control is something that is not included in even the latest versions of Builder or Delphi .  Indeed, when bound to a ListView control, the Header control is somewhat limited.  Adding Bitmaps to a ListView's Header control is quite simple, and is often preferred over making the Header owner-drawn, which is far more involved. 
 
KEYWORDS: Header_SetItem, HDF_BITMAP

 

 

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

//The goal is to change the style of the Header control to 
//accomodate a bitmap (HDF_BITMAP)... 

__fastcall TForm1::TForm1(TComponent* Owner) 
 : TForm(Owner) 

    //Get a handle to the header control
    HWND HeaderHandle = GetDlgItem(ListView1->Handle, 0);

    //Header control item structure
    HD_ITEM hdi;

    for (int index = 0; index < ListView1->Columns->Count; index++)
    {
        //Get the current format
        Header_GetItem(HeaderHandle, index, &hdi);

        hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_BITMAP | HDI_WIDTH;

        //Flag the bitmap format
        hdi.fmt = hdi.fmt | HDF_LEFT | HDF_BITMAP | HDF_STRING;
        hdi.pszText = ListView1->Columns->Items[index]->Caption.c_str();
        hdi.cchTextMax = ListView1->Columns->Items[index]->Caption.Length();
        hdi.cxy = ListView1->Columns->Items[index]->Width;
        
        switch(index)
        {
            case 0: hdi.hbm = Image1->Picture->Bitmap->Handle;
                    break;
            default: hdi.hbm = Image2->Picture->Bitmap->Handle;
                     break;
        }

        //Force the changes
        Header_SetItem(HeaderHandle, index, &hdi);
    }

 


Believe it or not, that's it to adding Bitmaps to the a ListView's Header Control... simple and effective.  If you need to change the size of the Header Control to accomodate larger bitmaps, then you'll have to change the Font to a larger size.  If you still want the smaller text, then simply add it into the Bitmap image, or make the Header owner-drawn.