Click here to Skip to main content
15,891,905 members
Articles / Desktop Programming / MFC
Article

A Multiline Header Control Inside a CListCtrl

Rate me:
Please Sign up or sign in to vote.
4.93/5 (13 votes)
16 May 2000 430.5K   7.5K   74   67
How to make the CListCtrl's header Multiline
  • Download source files - 18.8 Kb
  • Download demo project - 8 Kb

    Sample Image - HeaderCtrlEx.jpg

    First of all I have to mention that Alon Peleg helped me find the solution to the problem so I feel it is only fair that his name will be mentioned as an author.

    On a recent project I did I had to make the header control of a CListCtrl multiline. This small project show how to do it by subclassing the CHeaderCtrl of the CListCtrl.

    If you want to use this code just the HeaderCtrlExt.h and HeaderCtrlExt.cpp files into your source code.

    In addition on your CListView or CListCtrl derived class, add a member variable of type CHeaderCtrlEx and a member variable of type CFont.

    If you are using a CListCtrl without a view then put the following code in the end of the OnCreate handler of the CListCtrl:

    ///////////////////////SET UP THE MULTILINE HEADER CONTROL
    
    //m_NewHeaderFont is of type CFont
    m_NewHeaderFont.CreatePointFont(190,"MS Serif"); 
    
    CHeaderCtrl* pHeader = NULL;
    pHeader=GetHeaderCtrl();
    
    if(pHeader==NULL)
    	return;
    	
    VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd));	
    
    //A BIGGER FONT MAKES THE CONTROL BIGGER
    m_HeaderCtrl.SetFont(&m_NewHeaderFont);
    
    HDITEM hdItem;
    
    hdItem.mask = HDI_FORMAT;
    
    for(i=0; i < m_HeaderCtrl.GetItemCount(); i++)
    {
    	m_HeaderCtrl.GetItem(i,&hdItem);
    
    	hdItem.fmt|= HDF_OWNERDRAW;
    		
    	m_HeaderCtrl.SetItem(i,&hdItem);
    }

    If you are using a CListView or any class derived by it then add the following code in the OnInitialUpdate override of the CListView:

    ///////////////////////SET UP THE MULTILINE HEADER CONTROL
    //m_NewHeaderFont is of type CFont
    m_NewHeaderFont.CreatePointFont(190,"MS Serif"); 
    
    CListCtrl& ListCtrl = GetListCtrl();
    
    CHeaderCtrl* pHeader = NULL;
    pHeader=ListCtrl.GetHeaderCtrl();
    
    if(pHeader==NULL)
    	return;
    	
    VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd));	
    
    //A BIGGER FONT MAKES THE CONTROL BIGGER
    m_HeaderCtrl.SetFont(&m_NewHeaderFont);
    
    HDITEM hdItem;
    
    hdItem.mask = HDI_FORMAT;
    
    for(i=0; i < m_HeaderCtrl.GetItemCount(); i++)
    {
    	m_HeaderCtrl.GetItem(i,&hdItem);
    	hdItem.fmt|= HDF_OWNERDRAW;
    		
    	m_HeaderCtrl.SetItem(i,&hdItem);
    }

    The only difference between the two parts of code is way we get a pointer to the Header control.

    Thats it. Enjoy!

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Team Leader www.unitronics.com
    Israel Israel
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    Questionhow about in FormView? Pin
    TiianyMike3-Jul-11 23:34
    TiianyMike3-Jul-11 23:34 
    GeneralI want both multiline and dynamical update on header. [modified] Pin
    necoyam7-Jul-09 15:38
    necoyam7-Jul-09 15:38 
    GeneralDT_VCENTER not working Pin
    Erakis17-Jul-08 4:03
    Erakis17-Jul-08 4:03 
    Generalmanifest problem Pin
    marsiancba4-Apr-07 3:41
    marsiancba4-Apr-07 3:41 
    GeneralWindows XP theme Pin
    Farid Z13-Dec-06 14:17
    Farid Z13-Dec-06 14:17 
    GeneralRe: Windows XP theme Pin
    byung chul cho12-Jun-08 20:32
    byung chul cho12-Jun-08 20:32 
    Generalleft aligning Pin
    pedroo28-Oct-06 22:32
    pedroo28-Oct-06 22:32 
    QuestionCode in VB.NET Pin
    Rudis_DC25-Aug-06 0:41
    Rudis_DC25-Aug-06 0:41 
    AnswerRe: Code in VB.NET Pin
    Alberto Bar-Noy26-Aug-06 23:27
    Alberto Bar-Noy26-Aug-06 23:27 
    GeneralRe: Code in VB.NET Pin
    Rudis_DC17-Oct-06 1:20
    Rudis_DC17-Oct-06 1:20 
    GeneralRe: Code in VB.NET Pin
    Alberto Bar-Noy17-Oct-06 1:37
    Alberto Bar-Noy17-Oct-06 1:37 
    GeneralRe: Code in VB.NET Pin
    Rudis_DC17-Oct-06 1:43
    Rudis_DC17-Oct-06 1:43 
    Generalit is possible to have multiline column in row not header using win32 api Pin
    kcselvaraj17-Jul-06 4:31
    kcselvaraj17-Jul-06 4:31 
    GeneralRe: it is possible to have multiline column in row not header using win32 api Pin
    PurnimaKC9-Aug-06 20:16
    PurnimaKC9-Aug-06 20:16 
    Generalproblem with delete Pin
    cpicass30-Sep-05 4:45
    cpicass30-Sep-05 4:45 
    GeneralSeem does not work with SDI application Pin
    Member 104710930-Apr-04 3:37
    Member 104710930-Apr-04 3:37 
    GeneralRe: Seem does not work with SDI application Pin
    stephane.boeuf8-Jul-04 4:55
    stephane.boeuf8-Jul-04 4:55 
    GeneralRe: Seem does not work with SDI application Pin
    Rod Stone25-Jan-05 23:57
    Rod Stone25-Jan-05 23:57 
    I have the same problem with an MDI and a Splitter app. Refer to the post by mijob about using the HDM_LAYOUT message as this will resize the header without altering the font size. However, I also had a problem with this in that on startup the header was displayed in the usual default height and only drew itself correctly when the parent window was resized.

    It seems that the HDM_LAYOUT message is not sent immediately to the control unless certain properties are present in the HDITEM struct. I overcame this issue by first doing a setitem() on the first column with just the HDI_HEIGHT mask and a height value of zero. This appears to force a HDM_LAYOUT to be sent and if you previously set the number of lines in the header then the code outlined by mijob will adjust the header to the required height.

    Here is the code...

    // Get the header
    CHeaderCtrl* pHeader = NULL;
    pHeader = ListCtrl.GetHeaderCtrl ();

    if ( pHeader == NULL )
    return;

    // Do the subclassy bit
    VERIFY ( m_HeaderCtrl.SubclassWindow ( pHeader->m_hWnd ) );

    // Set the line count, its just a varialble in the
    // header control
    m_HeaderCtrl.SetLineCount ( 2 );

    // This should force an HDI_LAYOUT message
    HDITEM hdItem.mask = HDI_HEIGHT;
    hdItem.cxy = 0;
    m_HeaderCtrl.SetItem ( 0,&hdItem );

    // Set the rest of the column info, I include the width
    // here as well just to make sure!
    hdItem.mask = HDI_FORMAT | HDI_IMAGE | HDI_WIDTH;

    for ( i = 0; i < m_HeaderCtrl.GetItemCount (); i++ )
    {
    m_HeaderCtrl.GetItem ( i,&hdItem );
    hdItem.fmt |= HDF_OWNERDRAW;
    hdItem.cxy = _gnColumnWidth[i];
    m_HeaderCtrl.SetItem ( i,&hdItem );
    }

    // By the time you get here the header should be the correct height

    // HDM_LAYOUT handler in CHeaderCtrlExt
    LRESULT CHeaderCtrlEx::OnLayout ( WPARAM wParam, LPARAM lParam )
    {
    CClientDC dc ( this );
    CSize size = dc.GetTextExtent ( _T("test") );

    HDLAYOUT* pLayout = ( HDLAYOUT* ) lParam; // cast the lParam

    pLayout->pwpos->hwnd = GetSafeHwnd (); // populate WINDOWPOS struct
    pLayout->pwpos->hwndInsertAfter = NULL;
    pLayout->pwpos->flags = SWP_FRAMECHANGED;

    pLayout->pwpos->x = pLayout->prc->left;
    pLayout->pwpos->y = 0;
    pLayout->pwpos->cx = pLayout->prc->right - pLayout->prc->left;

    // Where nLineCount is set by the call to SetLineCount()
    // in this case, two
    int nHdrBottom = nLineCount * ( size.cy ) + 3;

    pLayout->pwpos->cy = nHdrBottom;

    pLayout->prc->top = nHdrBottom;

    return ( -1 );

    }

    Hope this helps Smile | :)
    GeneralThank you! Pin
    Jörgen Sigvardsson27-Feb-06 4:00
    Jörgen Sigvardsson27-Feb-06 4:00 
    QuestionCan be done in plain C ? Pin
    ednilsom16-Feb-04 3:07
    ednilsom16-Feb-04 3:07 
    QuestionIt doesn't work in a dialog? Pin
    wjlcqjy24-Jul-03 15:54
    wjlcqjy24-Jul-03 15:54 
    AnswerRe: It doesn't work in a dialog? Pin
    Jian Ruan29-Aug-03 15:41
    Jian Ruan29-Aug-03 15:41 
    GeneralRe: It doesn't work in a dialog? Pin
    Anonymous15-Oct-03 11:32
    Anonymous15-Oct-03 11:32 
    GeneralRe: It doesn't work in a dialog? Pin
    llllskywalker20-Oct-05 10:50
    llllskywalker20-Oct-05 10:50 
    AnswerRe: It doesn't work in a dialog? Pin
    llllskywalker20-Oct-05 13:09
    llllskywalker20-Oct-05 13:09 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.