Click here to Skip to main content
15,890,438 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.3K   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

     
    AnswerRe: xp visuals...? Pin
    soundman3223-Jul-03 22:09
    soundman3223-Jul-03 22:09 
    QuestionHow to bottons to list control... Pin
    17-Sep-01 22:46
    suss17-Sep-01 22:46 
    QuestionHow can I avoid some inconvenient side effects Pin
    12-Jun-01 23:33
    suss12-Jun-01 23:33 
    GeneralSub Colomns Pin
    8-Dec-00 23:53
    suss8-Dec-00 23:53 
    GeneralRe: Sub Colomns Pin
    Alberto Bar-Noy9-Dec-00 23:36
    Alberto Bar-Noy9-Dec-00 23:36 
    QuestionEquivalent in VB? Pin
    Amish Mehta30-Jun-00 8:13
    sussAmish Mehta30-Jun-00 8:13 
    GeneralCListView code doesn't work with VC 5.0 Pin
    C Morell30-Jun-00 6:14
    C Morell30-Jun-00 6:14 
    GeneralRe: CListView code doesn't work with VC 5.0 Pin
    Member 456321-Jul-00 5:01
    Member 456321-Jul-00 5:01 
    Generalproblem with the code Pin
    shiplu22-Jun-00 3:27
    shiplu22-Jun-00 3:27 
    GeneralRe: problem with the code Pin
    Attila27-Jun-00 21:39
    Attila27-Jun-00 21:39 
    GeneralRe: problem with the code Pin
    DakLozar10-Aug-00 6:41
    sussDakLozar10-Aug-00 6:41 
    GeneralRe: problem with the code Pin
    Alberto Bar-Noy3-Dec-00 1:56
    Alberto Bar-Noy3-Dec-00 1:56 
    GeneralRe: problem with the code Pin
    8-Feb-01 4:12
    suss8-Feb-01 4:12 
    GeneralProject Files Pin
    richard17-May-00 12:57
    richard17-May-00 12:57 
    GeneralRe: Project Files Pin
    Alberto Gattegno21-May-00 23:41
    Alberto Gattegno21-May-00 23:41 
    GeneralCompile Errors Pin
    John E Katich14-May-00 14:36
    John E Katich14-May-00 14:36 
    GeneralRe: Compile Errors Pin
    Alberto Gattegno16-May-00 5:25
    Alberto Gattegno16-May-00 5:25 

    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.