Click here to Skip to main content
15,886,806 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.1K   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

     
    GeneralRe: It doesn't work in a dialog? Pin
    llllskywalker7-Mar-11 6:20
    llllskywalker7-Mar-11 6:20 
    GeneralGetting Assertion when used in my Framework Pin
    S Ganapathi Raman4-Jul-03 22:59
    S Ganapathi Raman4-Jul-03 22:59 
    GeneralRe: Getting Assertion when used in my Framework Pin
    Alberto Bar-Noy6-Jul-03 1:41
    Alberto Bar-Noy6-Jul-03 1:41 
    Question:) That's really a good idea,but could u draw the two line text with two different fonts? Pin
    skygg3-Jun-03 16:16
    skygg3-Jun-03 16:16 
    AnswerRe: :) That's really a good idea,but could u draw the two line text with two different fonts? Pin
    Alberto Bar-Noy4-Jun-03 1:23
    Alberto Bar-Noy4-Jun-03 1:23 
    Questionhi,i just can not understand how can this piece of code make the Header Control Multiline? Pin
    skygg2-Jun-03 20:15
    skygg2-Jun-03 20:15 
    AnswerRe: hi,i just can not understand how can this piece of code make the Header Control Multiline? Pin
    Alberto Bar-Noy2-Jun-03 21:27
    Alberto Bar-Noy2-Jun-03 21:27 
    Questioncan u make it looks like msn friend lists? Pin
    skygg19-May-03 23:23
    skygg19-May-03 23:23 
    AnswerRe: can u make it looks like msn friend lists? Pin
    Alberto Bar-Noy19-May-03 23:36
    Alberto Bar-Noy19-May-03 23:36 
    GeneralRe: can u make it looks like msn friend lists? Pin
    skygg1-Jun-03 23:38
    skygg1-Jun-03 23:38 
    GeneralRe: can u make it looks like msn friend lists? Pin
    AntonDel12-Aug-03 9:45
    AntonDel12-Aug-03 9:45 
    QuestionHelp:How to handle it in dialog based project? Pin
    sages7-Mar-03 13:56
    sages7-Mar-03 13:56 
    AnswerRe: Help:How to handle it in dialog based project? Pin
    Alberto Bar-Noy8-Mar-03 21:31
    Alberto Bar-Noy8-Mar-03 21:31 
    GeneralRe: Help:How to handle it in dialog based project? Pin
    sages9-Mar-03 15:21
    sages9-Mar-03 15:21 
    GeneralRe: Help:How to handle it in dialog based project? Pin
    Alberto Bar-Noy10-Mar-03 6:26
    Alberto Bar-Noy10-Mar-03 6:26 
    GeneralHow to change the height of header? Pin
    sages10-Mar-03 20:45
    sages10-Mar-03 20:45 
    Generalfix for MFC ver 7 Pin
    19-Jan-03 5:54
    suss19-Jan-03 5:54 
    GeneralYou are not as silly as you look! Pin
    Joe Sonderegger24-Feb-04 23:56
    Joe Sonderegger24-Feb-04 23:56 
    GeneralColumn labels without '\n' characters Pin
    VladoB5-Dec-02 1:38
    VladoB5-Dec-02 1:38 
    GeneralJust using listctrl... one way to do it... Pin
    Adam Bialowas26-Oct-02 10:18
    Adam Bialowas26-Oct-02 10:18 
    Questionhow can I paint CListCtrl with custom color Pin
    fyl_jf25-Jul-02 22:10
    fyl_jf25-Jul-02 22:10 
    AnswerRe: how can I paint CListCtrl with custom color Pin
    Alberto Bar-Noy19-Oct-02 21:36
    Alberto Bar-Noy19-Oct-02 21:36 
    GeneralMulti lines in the columns of the Control Pin
    5-Oct-01 18:19
    suss5-Oct-01 18:19 
    GeneralRe: Multi lines in the columns of the Control Pin
    Alberto Bar-Noy6-Oct-01 21:52
    Alberto Bar-Noy6-Oct-01 21:52 
    Questionxp visuals...? Pin
    28-Sep-01 6:20
    suss28-Sep-01 6:20 

    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.