Click here to Skip to main content
15,851,090 members
Articles / Desktop Programming / MFC

Changing Row Height in an owner drawn Control

Rate me:
Please Sign up or sign in to vote.
4.89/5 (25 votes)
8 Nov 2001CPOL4 min read 357.8K   6.7K   88   41
Changing the Row Height in an owner drawn List View Control

Table of Contents


2001-10-10 Inserted example "" from Danielle.

2001-09-27 Created article


This article is a close adaption of the article "Changing row height in owner drawn control" at Codeguru. I've enhanced as much as I can while trying to keep as much original content as possible. I also included a summary of the article's comments.


When you change the font of a list view control, the control or its parent window does not get a chance to respecify the height of the rows. No

<a href="">WM_MEASUREITEM</a>
message is sent to the controls parent window. The net effect is that when the font is changed for the control, the row height is no longer valid.

Here's a work around that forces the WM_MEASUREITEM message to be generated


The code in the following steps uses MFC and its class CListCtrl.

Step 1: Add handler for WM_SETFONT

The WM_MEASUREITEM message is sent when the control is created. Fortunately this message is also sent whenever the control is resized. Since we want the row height to be adjusted when the font changes, what better place to do thin than in the

<a href="">WM_SETFONT</a>
handler. In
<a href="">OnSetFont()</a>
we fool Windows into thinking that the window size of the control has changed.

We do this by sending the

message to the control which then gets handled by the default window procedure for the control. Note that we haven't specified the SWP_NOSIZE flag and we set the width and height field in the
<a href="">WINDOWPOS</a>
structure equal to the existing dimension.

In the header file:

afx_msg LRESULT OnSetFont(WPARAM wParam, LPARAM);
afx_msg void MeasureItem ( LPMEASUREITEMSTRUCT lpMeasureItemStruct );

In the CPP file:


    LRESULT res = Default();

    CRect rc;
    GetWindowRect( &rc );

    wp.hwnd  = m_hWnd;    = rc.Width();    = rc.Height();
    SendMessage( WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp );

    return res;

Step 2: Add handler for WM_MEASUREITEM

Since we want our CListCtrl derived class to be modular, we will handle the WM_MEASUREITEM message within this class. The message however, is sent to the parent window, so we use message reflection. Again, the Class Wizard is not much help. We have to manually add the entry in the message map and update the header file. See the code snippet in step one for this.

void CMyListCtrl::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
    LOGFONT lf;
    GetFont()->GetLogFont( &lf );
    if ( lf.lfHeight < 0 )
        lpMeasureItemStruct->itemHeight = -lf.lfHeight; 
        lpMeasureItemStruct->itemHeight = lf.lfHeight; 


So far the steps, adapted from the original article. Following is a summary of the most interesting comments at the original article. The comments include fixes and corrections to the previous steps. Since I want to leave the article as it is, I decided to not include suggested fixes in the code fragments above. It's up to you to use or not to use them. Another reason is, that I don't know whether all suggested fixes are correct. Please drop a comment here so that I can remove/adjust any errors.

Readers Comments

Proper implementation of MeasureItem() routine

When setting the row height for the CListCtrl derived object in the MeasureItem() routine, you shouldn't use the lfHeight member of the LOGFONT structure. This value is a logical point size value, not a physical pixels value. With different screen resolutions and larger font values, the row height will still be wrong. The following code shows an example for implementing the MeasureItem() routine that should work for all of these cases, and produces the exact same height as the default implementation.

void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
    HDC hDC = ::GetDC(NULL);
    CFont* pFont = GetFont();
    HFONT hFontOld = (HFONT)SelectObject(hDC, pFont->GetSafeHandle());
    GetTextMetrics(hDC, &tm);
    lpMeasureItemStruct->itemHeight = tm.tmHeight + tm.tmExternalLeading + 1;
    SelectObject(hDC, hFontOld);
    ::ReleaseDC(NULL, hDC);

(Paul Geringer, 1999-04-06 )

I don't receive a WM_SETFONT or WM_MEASUREITEM message

I don't receive a WM_SETFONT or WM_MEASUREITEM message.

(Damian Williams, 1999-10-18)


  1. Modify the style of the list control to "owner draw fixed" (LVS_OWNERDRAWFIXED).
  2. Overload CListCtrlEx::DrawItem().
  3. A WM_MEASUREITEM message will be sent to a parent window of list control.

(Chung Hyun, Lee, 2000-05-25)

Solution 2:
Class Wizard does not show you the =WM_MEASUREITEM reflected message. If you use Class Wizard to get WM_MEASUREITEMS, it can only be handled by a class other than the list view control, i.e. a parent window. This is not satisfactory. To get a reflected message you must add the following in the message map in your .CPP file:


Then you must add the following in your header file message map:

afx_msg void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct );

then add the following function in your .CPP file:

void CMyListCtrl::MeasureItem ( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
    // this should happen when the control is created.
    AfxMessageBox("You just received a WM_MEASUREITEM message");

(Dominic Gamble, 2000-01-13)

Solution 3:
First you must create the list view control with the style LVS_OWNERDRAWFIXED. Then, change the control's size, using the CWnd::MoveWindow() or CWnd::SetWindowPos() function.

It works both with hand-made-controls (using call a call to CListCtrl::Create()) and template-made-controls (using the resource editor)

(Gon (Jung-gon, Kim), 2000-08-09)

Changing the height of a row without using LVS_OWNERDRAWFIXED

You can easily change the row height by making the image list icon size to your desired size (and fool the user by drawing your icon not to the actual size). E.g.:

imagelist.Create( 24, 24, ILC_COLOR4, 10, 10 ); 
m_cList.SetImageList( &imageList, LVSIL_SMALL ); 

If your icon has 16x16, you make your row height 50% bigger. This seems awkward, but it works with ease.

(Allan Gallano, 2000-11-17)

See also/References

  1. Changing row height in owner drawn control, Zafir Anjum, 1998-08-06,
  2. Selection Highlighting of an Entire Row, Uwe Keim, 2001-09-26,
  3. Neat Stuff to do in List Controls Using Custom Draw, Michael Dunn, 1999-11-30,


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  

Comments and Discussions

GeneralRe: Anfrage Pin
Uwe Keim17-Feb-04 0:29
sitebuilderUwe Keim17-Feb-04 0:29 
GeneralRe: Anfrage Pin
rracer11-Feb-05 5:47
rracer11-Feb-05 5:47 
Questionhow to add check boxes in sub items Pin
girishnadig9-Sep-03 0:59
girishnadig9-Sep-03 0:59 
Generaldoesn't work in Small Icon View Pin
Tut817-Jun-03 22:11
Tut817-Jun-03 22:11 
fjlaga_old29-Aug-02 16:02
fjlaga_old29-Aug-02 16:02 
GeneralAWESOME! Pin
Swinefeaster1-Aug-02 20:41
Swinefeaster1-Aug-02 20:41 
GeneralIf no WM_MEASUREITEM message Pin
Steve Prescott15-Jul-02 9:52
Steve Prescott15-Jul-02 9:52 
GeneralRe: If no WM_MEASUREITEM message Pin
Skarrin22-Oct-03 2:13
Skarrin22-Oct-03 2:13 
I had the same problem and ended up putting a
SetFont(GetFont(), FALSE);
at the beginning of my DrawItem handler.

Works for me Roll eyes | :rolleyes: !

Regards, Jens

Patriotism is a pernicious, psychopathic form of idiocy.
-George Bernard Shaw-
GeneralRe: If no WM_MEASUREITEM message Pin
Siddharth R Barman23-Apr-15 20:19
Siddharth R Barman23-Apr-15 20:19 
QuestionMultiline List View? Pin
9-Oct-01 23:52
suss9-Oct-01 23:52 
AnswerRe: Multiline List View? Pin
Uwe Keim13-Dec-01 22:17
sitebuilderUwe Keim13-Dec-01 22:17 
GeneralDemo Apps Pin
30-Sep-01 13:13
suss30-Sep-01 13:13 
GeneralRe: Demo Apps Pin
Uwe Keim30-Sep-01 20:04
sitebuilderUwe Keim30-Sep-01 20:04 
GeneralRe: Demo Apps Pin
30-Sep-01 23:17
suss30-Sep-01 23:17 
GeneralRe: Demo Apps Pin
Uwe Keim30-Sep-01 23:51
sitebuilderUwe Keim30-Sep-01 23:51 
GeneralRe: Demo Apps Pin
Igor Okulist10-Oct-01 10:30
Igor Okulist10-Oct-01 10:30 
GeneralHey, what's your point? Pin
Uwe Keim10-Oct-01 10:39
sitebuilderUwe Keim10-Oct-01 10:39 

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.