Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1396
  • Last Modified:

Font Size CHeaderCtrl

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.
  • 3
  • 3
  • 2
1 Solution
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:


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
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);

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);

rect.top += height;
m_headerImageList.Draw(pdc, pdis->iImage, rect.TopLeft(), ILD_NORMAL);


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

cophiAuthor Commented:
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?
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.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/header/structures/hditem.asp <==

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 : http://www.codeproject.com/listctrl/headerctrlex.asp <==

< 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.
I have just seen a typing mistake in my original comment.  
SetFopnt should be SetFont.
cophiAuthor Commented:
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now