dcsimg

Displaying an image in the header

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps



Unlike most other common controls, the header control does not use an image list. It does allow you to use a bitmap in any of the header item. You can display just the bitmap or the bitmap with the item label. You use the CHeaderCtrl::SetItem() function to assign a bitmap to a header item or to remove a bitmap if one is already assigned.

The following code assigns a bitmap to a header control item. This code will show the bitmap as well as the item label. I noticed one bug with this code. The right most column of pixels in the bitmap does not get displayed. You should, therefore, not consider the right most column when creating the bitmap. Also, if the bitmap is taller than what the header control can display, then only the middle portion of the bitmap is displayed. The header control does not have a problem in displaying wide bitmaps. Another note of warning, the bitmap is not displayed transparently. If you want a non rectangular shape, you have to use a gray background for the bitmap and hope that the user does not change the color scheme.

	HD_ITEM hditem;
	hditem.mask = HDI_FORMAT;
	((CHeaderCtrl*)GetDlgItem(0))->GetItem( nCol, &hditem );
	hditem.mask = HDI_BITMAP | HDI_FORMAT;
	hditem.fmt |= HDF_BITMAP;
	// mybitmap is a CBitmap object containing the bitmap
	hditem.hbm = (HBITMAP)mybitmap.GetSafeHandle();
	((CHeaderCtrl*)GetDlgItem(0))->SetItem( nCol, &hditem );

If you want to display just the bitmap and not the label, then you can remove the HDF_STRING flag from the fmt field.
	HD_ITEM hditem;
	hditem.mask = HDI_FORMAT;
	((CHeaderCtrl*)GetDlgItem(0))->GetItem( nCol, &hditem );
	hditem.mask = HDI_BITMAP | HDI_FORMAT;
	hditem.fmt |= HDF_BITMAP;
	hditem.fmt &= ~HDF_STRING ;
	// mybitmap is a CBitmap object containing the bitmap
	hditem.hbm = (HBITMAP)mybitmap.GetSafeHandle();
	((CHeaderCtrl*)GetDlgItem(0))->SetItem( nCol, &hditem );

To remove a bitmap from the column header, you can change the format by calling the SetItem() function again and clear the HDF_BITMAP flag from the format field. Alternatively, you can use HDI_BITMAP as the mask and set the hbm field to NULL.

	HD_ITEM hditem;
	hditem.mask = HDI_FORMAT;
	((CHeaderCtrl*)GetDlgItem(0))->GetItem( nCol, &hditem );
	hditem.mask = HDI_FORMAT;
	hditem.fmt &= ~HDF_BITMAP;
	((CHeaderCtrl*)GetDlgItem(0))->SetItem( nCol, &hditem );



Comments

  • How to add a picture to a subitem

    Posted by Legacy on 09/04/2003 12:00am

    Originally posted by: Tiger

    How can i add a picture to a specific subitem in the list control, like you do it in the header control?
    
    

    any help would be very appreciated
    tiger

    Reply
  • Didn't work at the first time. Complete method attached....

    Posted by Legacy on 07/31/2003 12:00am

    Originally posted by: Frenzy

    Hi!
    
    

    I had some trouble at the first time with the code. So I changed it at some parts. Here is now my working method for adding a bitmap to the Header of a CListCtrl.

    Frenzy

    void CBilView::setListCtrlHeaderBitmap( CListCtrl* pList, int iColumn, BOOL bUpDown )
    {
    HINSTANCE hInst = AfxGetInstanceHandle();
    HBITMAP hBitmap = NULL;
    HD_ITEM hditem;

    CString strText = "";

    CWnd * pChild = pList->GetWindow( GW_CHILD ) ;
    if( pChild == NULL )
    {
    return ;
    }
    ASSERT_VALID(pChild) ;

    // load Bitmap
    if( bUpDown == TRUE )
    {
    hBitmap = ::LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP_ARR_UP));
    }
    else
    {
    hBitmap = ::LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP_ARR_DOWN));
    }

    // get HeaderCtrl
    CHeaderCtrl* pListHeader = pList->GetHeaderCtrl();

    // get header
    char szText[256] ;
    memset( &hditem, 0, sizeof hditem ) ;
    hditem.mask = HDI_TEXT ;
    hditem.pszText = szText ;
    hditem.cchTextMax = sizeof szText ;
    pChild->SendMessage( HDM_GETITEM, iColumn, (LPARAM)&hditem );
    strText = szText;

    // set header and bitmap
    hditem.mask = HDI_FORMAT;
    BOOL bRes = pListHeader->GetItem( iColumn, &hditem );
    hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_BITMAP;
    hditem.fmt |= HDF_BITMAP;
    hditem.hbm = hBitmap;
    hditem.pszText = strText.GetBuffer(strText.GetLength());
    hditem.cchTextMax = strText.GetLength();
    bRes = pListHeader->SetItem( iColumn, &hditem );

    // reset of all other columns
    int iCount = pListHeader->GetItemCount();

    for( int i=0; i<iCount; i++ )
    {
    if( i != iColumn )
    {
    // get header
    szText[256] ;
    memset( &hditem, 0, sizeof hditem ) ;
    hditem.mask = HDI_TEXT ;
    hditem.pszText = szText ;
    hditem.cchTextMax = sizeof szText ;
    pChild->SendMessage( HDM_GETITEM, i, (LPARAM)&hditem );
    strText = szText;

    // set header
    hditem.mask = HDI_FORMAT;
    bRes = pListHeader->GetItem( i, &hditem );
    hditem.mask = HDI_FORMAT;
    hditem.fmt &= ~HDF_BITMAP;
    hditem.pszText = strText.GetBuffer(strText.GetLength());
    hditem.cchTextMax = strText.GetLength();
    bRes = pListHeader->SetItem( i, &hditem );
    }
    }

    return;
    }

    Reply
  • How to display the icon( ! ) in the center of the column?

    Posted by Legacy on 01/21/2002 12:00am

    Originally posted by: kalpana

    hello,
    i want to display an icon ! in the center of the column.For
    drawing columns we are using listview control..it is not ownerdrwan ...thats why i am gettting problem..is there any way to do this...?
    and second thing is i want to select(highlight) all the columns in a row except the first one.
    so please help me out in this
    thank you,
    kalpana

    Reply
  • Windows XP Icon spacing

    Posted by Legacy on 01/19/2002 12:00am

    Originally posted by: Denis Galiana

    Hello,

    I've noticed in Windows XP that the gap between icons in 2nd, 3rd, ... columns and text is greater (al least 3 times more) that the gap between the first column icon and the first column text ....

    Reply
  • Displaying an image in the header

    Posted by Legacy on 07/07/1999 12:00am

    Originally posted by: Dale King

    I don't seem to get an image included within the header. I only get some blank header space. Any thoughts?

    Reply
  • Displaying and image in header control

    Posted by Legacy on 02/09/1999 12:00am

    Originally posted by: Joe Holloway


    RE: If you want a non rectangular shape, you have to use a gray background for the bitmap and hope that the user does not change the color scheme.

    One can use the CBitmap member function LoadMappedBitmap to get around this. If you create a bitmap with the standard gray windows background and load it using LoadMappedBitmap, it will automagically map that gray color to the current background color. Use the help file to learn more!

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • CEOs, CIOs, boards and shareholders are demanding digital transformation. They want their organizations to be more customer-focused, competitive and strategic to increase revenue. Data drives all of those aspects. A great first step in optimizing data use is moving data to the cloud. It can quickly show benefits. Here are six aspects of a cloud data management strategy that will help your organization more fully move, manage and use valuable data to successfully support digital transformation.

  • Hybrid IT consists of both cloud and on-premises data center infrastructure. This book helps you understand both sides of the hybrid IT equation and how HPE can help your organization transform its IT operations and save time and money in the process. I delve into the worlds of security, economics, and operations to show you new ways to support your business workloads.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.