Click here to Skip to main content
11,409,532 members (62,299 online)
Click here to Skip to main content

A Multiline Header Control Inside a CListCtrl

, 16 May 2000
Rate this:
Please Sign up or sign in to vote.
How to make the CListCtrl's header Multiline
<!-- Link to source file download -->
  • Download source files - 18.8 Kb <!-- Link to demo file download -->
  • Download demo project - 8 Kb <!-- Article image -->

    Sample Image - HeaderCtrlEx.jpg

    <!-- Add the rest of your HTML here -->

    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

    Share

    About the Author

    Alberto Bar-Noy
    Team Leader www.unitronics.com
    Israel Israel
    No Biography provided

    Comments and Discussions

     
    Questionhow about in FormView? Pinmember田原 from Xidian4-Jul-11 0:34 
    GeneralI want both multiline and dynamical update on header. [modified] Pinmembernecoyam7-Jul-09 16:38 
    GeneralDT_VCENTER not working PinmemberErakis17-Jul-08 5:03 
    Generalmanifest problem Pinmembermarsiancba4-Apr-07 4:41 
    GeneralWindows XP theme PinmemberFarid Zidan13-Dec-06 15:17 
    GeneralRe: Windows XP theme Pinmemberbyung chul cho12-Jun-08 21:32 
    Generalleft aligning Pinmemberpedroo28-Oct-06 23:32 
    QuestionCode in VB.NET PinmemberRudis_DC25-Aug-06 1:41 
    AnswerRe: Code in VB.NET PinmemberAlberto Bar-Noy27-Aug-06 0:27 
    GeneralRe: Code in VB.NET PinmemberRudis_DC17-Oct-06 2:20 
    GeneralRe: Code in VB.NET PinmemberAlberto Bar-Noy17-Oct-06 2:37 
    GeneralRe: Code in VB.NET PinmemberRudis_DC17-Oct-06 2:43 
    Generalit is possible to have multiline column in row not header using win32 api Pinmemberkcselvaraj17-Jul-06 5:31 
    GeneralRe: it is possible to have multiline column in row not header using win32 api PinmemberPurnimaKC9-Aug-06 21:16 
    Generalproblem with delete Pinmembercpicass30-Sep-05 5:45 
    GeneralSeem does not work with SDI application Pinmemberx-algo30-Apr-04 4:37 
    GeneralRe: Seem does not work with SDI application Pinmemberstephane.boeuf8-Jul-04 5:55 
    GeneralRe: Seem does not work with SDI application PinmemberRod Stone26-Jan-05 0:57 
    GeneralThank you! PinmemberJörgen Sigvardsson27-Feb-06 5:00 
    QuestionCan be done in plain C ? Pinsussednilsom16-Feb-04 4:07 
    QuestionIt doesn't work in a dialog? Pinmemberwjlcqjy24-Jul-03 16:54 
    AnswerRe: It doesn't work in a dialog? PinmemberJian Ruan29-Aug-03 16:41 
    GeneralRe: It doesn't work in a dialog? PinsussAnonymous15-Oct-03 12:32 
    GeneralRe: It doesn't work in a dialog? Pinmemberllllskywalker20-Oct-05 11:50 
    AnswerRe: It doesn't work in a dialog? Pinmemberllllskywalker20-Oct-05 14:09 
    GeneralRe: It doesn't work in a dialog? Pinmemberllllskywalker7-Mar-11 7:20 
    GeneralGetting Assertion when used in my Framework PinmemberS Ganapathi Raman4-Jul-03 23:59 
    GeneralRe: Getting Assertion when used in my Framework PinmemberAlberto Gattegno6-Jul-03 2:41 
    Question:) That's really a good idea,but could u draw the two line text with two different fonts? Pinmemberskygg3-Jun-03 17:16 
    AnswerRe: :) That's really a good idea,but could u draw the two line text with two different fonts? PinmemberAlberto Gattegno4-Jun-03 2:23 
    Questionhi,i just can not understand how can this piece of code make the Header Control Multiline? Pinmemberskygg2-Jun-03 21:15 
    AnswerRe: hi,i just can not understand how can this piece of code make the Header Control Multiline? PinmemberAlberto Gattegno2-Jun-03 22:27 
    Questioncan u make it looks like msn friend lists? Pinmemberskygg20-May-03 0:23 
    AnswerRe: can u make it looks like msn friend lists? PinmemberAlberto Gattegno20-May-03 0:36 
    GeneralRe: can u make it looks like msn friend lists? Pinmemberskygg2-Jun-03 0:38 
    GeneralRe: can u make it looks like msn friend lists? PinmemberAntonDel12-Aug-03 10:45 
    QuestionHelp:How to handle it in dialog based project? Pinsusssages7-Mar-03 14:56 
    AnswerRe: Help:How to handle it in dialog based project? PinmemberAlberto Gattegno8-Mar-03 22:31 
    GeneralRe: Help:How to handle it in dialog based project? Pinsusssages9-Mar-03 16:21 
    GeneralRe: Help:How to handle it in dialog based project? PinmemberAlberto Gattegno10-Mar-03 7:26 
    GeneralHow to change the height of header? Pinsusssages10-Mar-03 21:45 
    Generalfix for MFC ver 7 Pinmembermijoh19-Jan-03 6:54 
    GeneralYou are not as silly as you look! PinmemberNice Life25-Feb-04 0:56 
    GeneralColumn labels without '\n' characters Pinmembervladob5-Dec-02 2:38 
    GeneralJust using listctrl... one way to do it... PinsussAdam Bialowas26-Oct-02 11:18 
    Questionhow can I paint CListCtrl with custom color Pinmemberfyl_jf25-Jul-02 23:10 
    AnswerRe: how can I paint CListCtrl with custom color PinmemberAlberto Gattegno19-Oct-02 22:36 
    GeneralMulti lines in the columns of the Control PinmemberUdayini5-Oct-01 19:19 
    GeneralRe: Multi lines in the columns of the Control PinmemberAlberto Gattegno6-Oct-01 22:52 
    Questionxp visuals...? PinmemberSafai28-Sep-01 7:20 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150414.5 | Last Updated 17 May 2000
    Article Copyright 2000 by Alberto Bar-Noy
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid