MeasureItem for dynamic font changing in a list control | CodeGuru

MeasureItem for dynamic font changing in a list control

Zafir’s article on adjusting row height in a CListCtrl derived class needs to be adjusted when dealing with dynamic font changes. His sample causes some problems where the row height is not correctly computed to compensate for different video display modes. The code snip below responds correctly to font changes by selecting the font object […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 11, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Zafir’s article on adjusting row height in a CListCtrl derived class needs to be
adjusted when dealing with dynamic font changes. His sample causes some problems where
the row height is not correctly computed to compensate for different video display
modes.

The code snip below responds correctly to font changes by selecting the font object
into the Client Dc, then obtains the text metrics. Note that we are adding tmHeight
and tmExternalLeading values to cover the glyphs that extend beyond the normal height
boundaries of a given font.

void CMyListCtrl::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
	CClientDC dc( this );
	CFont* pFont = GetFont();
	ASSERT( pFont );

	CFont* pOldFont = dc.SelectObject( pFont );
	ASSERT( pOldFont );

	TEXTMETRIC txtMetric;
	BOOL bRet = dc.GetTextMetrics( &txtMetric );
	ASSERT( bRet );

	int cyChar = txtMetric.tmHeight + txtMetric.tmExternalLeading;
	ASSERT( 0 != cyChar );

	lpMeasureItemStruct->itemHeight = cyChar;
}

Last updated: 29 July 1998 by Rob Osborne

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.