Click here to Skip to main content
15,893,814 members
Articles / Desktop Programming / MFC
Article

A Multiline Header Control Inside a CListCtrl

Rate me:
Please Sign up or sign in to vote.
4.95/5 (14 votes)
16 May 2000 430.6K   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: 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 
    I got an email response to this article as follows:

    Adam,

    I am pulling my hair out with this code. It looks very cool, but I can't get it to work in a CFormView. By chance, could you help me put together a demo project of this multiline header control for a list control in a CFormView rather than the CListView? Please, please.

    Any response you can give me will be greatly appreciated.

    Sincerely,
    **Censored**

    I thought sure... no problems... and here is what I did...

    I overwrote List control and added a single function:

    void MLHListCtrl::InitControlHeadings(CStringArray &myColumnHeaders)
    {
    // Here I figure out the column sizes and populate the columns
    RECT rect;
    GetClientRect(&rect);
    int colWidth = (int)(rect.right - rect.left)/myColumnHeaders.GetSize();
    for(int i = 0; i < myColumnHeaders.GetSize(); i++)
    InsertColumn(i,myColumnHeaders.GetAt(i),LVCFMT_CENTER,colWidth);

    // **** SET UP MULTILINE HEADERS **** \/
    InsertItem(0,"DELETE!"); // Used to trick the column bar to refresh

    CHeaderCtrl* pHeader = NULL;
    pHeader = GetHeaderCtrl();
    if(pHeader==NULL) return;// FALSE;
    VERIFY(m_ctrlHeader.SubclassWindow(pHeader->m_hWnd));

    //m_NewHeaderFont is of type CFont
    m_cfontNewHeaderFont.CreatePointFont(390,"MS Serif"); //190 for two rows of text
    m_ctrlHeader.SetFont(&m_cfontNewHeaderFont);

    HDITEM hdItem;
    hdItem.mask = HDI_FORMAT;
    for(i=0; i < m_ctrlHeader.GetItemCount(); i++)
    {
    m_ctrlHeader.GetItem(i,&hdItem);
    hdItem.fmt|= HDF_OWNERDRAW;
    m_ctrlHeader.SetItem(i,&hdItem);
    }

    DeleteItem(0); // Completing the trick requires removing the grabage!
    // **** SET UP MULTILINE HEADERS **** ^
    }

    Note that I had private member vairables:
    CFont m_cfontNewHeaderFont;
    CHeaderCtrlEx m_ctrlHeader;


    Then in the dialog, I added a handler for WM_INITDIALOG and then did the following:
    CStringArray ColumnHeader;
    ColumnHeader.Add("Column 1\nLine 1\nLine 2\nLine 3\nLine 4");
    ColumnHeader.Add("Column 2\nLine 1\nLine 2\nLine 3\nLine 4");
    ColumnHeader.Add("Column 3\nLine 1\nLine 2\nLine 3\nLine 4");
    ColumnHeader.Add("Column 4\nLine 1\nLine 2\nLine 3\nLine 4");
    myList.InitControlHeadings(ColumnHeader);

    Where myList is a member variable for my overwritten control.

    This is just a quick hack, probably could make it nicer, but just wanted to show how easy it was to use this class in the control as well as the view.

    Hope this is useful.

    Cheers,

    Adam Big Grin | :-D
    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 
    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 

    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.