Font Size CHeaderCtrl

Posted on 2006-05-09
Last Modified: 2013-12-03
How do I change the font type for the CHeaderCtrl.  I want it to be alot bigger than it is right now.  I would like it to be bigger than the item text in the CListCtrl.
Question by:cophi
    LVL 22

    Expert Comment

    Have you tried it with CWnd::SetFont()..

    In your CListView derived class, override OnCreate( ) and do the following:

        CWnd *pHeader = GetDlgItem(0);    // This gets you a pointer to the CHeaderCtrl

    Also you may have a look at this full thread discussing about it with code:


    LVL 43

    Accepted Solution

    In header file
    CFont m_Font;

    in .cpp file
    m_Font.Create(...);  //create the font you want to use
    m_lstCtrl.GetHeaderCtrl()->SetFopnt(&m_Font);  //set the font in the header of the list control
    LVL 22

    Expert Comment

    As disussed in above thread :

    How to change the height of Header in CListCtrl


    Here's an approach that might work for you;

    1) Choose a font size with height approximately what you want the header to be, then set the header font to it - something like:

    // one time initialization
    m_sizingFont.CreatePointFont(180, "MS Sans Serif");
    m_normalFont.CreatePointFont(80, "MS Sans Serif");

    CHeaderCtrl * pHdrCtrl = m_listCtrl.GetHeaderCtrl();


    2) Make each header item owner draw.
    // for each column do something like the following:
    HD_ITEM curItem;

    pHdrCtrl->GetItem(column, &curItem);
    curItem.mask = HDI_FORMAT;
    curItem.fmt = HDF_OWNERDRAW;

    pHdrCtrl->SetItem(column, &curItem);

    3) Finally, Draw header items, which may be trivial or complex depending on your applkication. The following snippet merely uses whatever was set in the each header item, which you have to do anyway.

    void MyListCtrl::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT pdis )
    ASSERT(pdis->CtlType == ODT_HEADER);

    CDC * pdc = CDC::FromHandle(pdis->hDC);
    CFont * pOldFont = pdc->SelectObject(&m_normalFont);

    LVCOLUMN col;
    char buff[64];

    col.mask = LVCF_IMAGE | LVCF_TEXT;
    col.pszText = buff;
    col.cchTextMax = sizeof(buff);

    GetColumn(pdis->itemID, &col);

    CRect rect(pdis->rcItem);
    rect.left += 2;
    int height = pdc->DrawText(buff, rect, DT_SINGLELINE | DT_LEFT | DT_TOP | DT_END_ELLIPSIS); += height;
    m_headerImageList.Draw(pdc, pdis->iImage, rect.TopLeft(), ILD_NORMAL);



    Author Comment

    The first 2 solutions do not work, this last solution might work but I'd really not like to override the draw function, any other solutiosn?
    LVL 22

    Expert Comment

    Code like following should work with CreatePointFont and MS Sans Serief but you need to set Header Control Size appropriate depending on font size :

    CFont m_FontHeader; // Make this member of your class
    m_FontHeader.CreatePointFont(180, "MS Sans Serif");

    CHeaderCtrl* pHeader = m_ListCtrl.GetHeaderCtrl();

    to resize your header control you may use HDITEM struct. <==

    you may set HDITEM.mask to HDI_WIDTH, HDI_HEIGHT and then pHeader->SetItem() to change header control size.

    e.g after setting font of header control you may set height like

    HDITEM hdItem;

    hdItem.mask = HDI_HEIGHT;
    hdItem.cxy = 40; // set height

    for( int i = 0; i < pHeader->GetItemCount(); i++ )
      pHeader->SetItem( i, &hdItem );

    Have a look at here for sample code : <==

    LVL 43

    Expert Comment

    < The first 2 solutions do not work>

    I have just tested my suggestion here, 3 lines of code, it DOES WORK.  Do you want a screenshot of it?

    Have you followed my instructions?
    Please post your code.
    LVL 43

    Expert Comment

    I have just seen a typing mistake in my original comment.  
    SetFopnt should be SetFont.

    Author Comment

    Yeah, Apparantly they do work.  My ListCtrl, everytime it redrew itself, it would overwrite the font, with the normal system font.  I guess I just had the code in the wrong place.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now