A Journey 2 Eternity

Draw vertically centered multi-line text in a given rectangle

Posted on: April 22, 2009

DT_WORDBREAK and DT_VCENTER flag never work together. So here is an alternative.

void CEMCHotButton::DrawCaption (CDC* pDC, LPDRAWITEMSTRUCT lpDrawItemStruct, CRect& rectCaption, DWORD dwFormat) 
{
	// Get caption text
	CString strCaption;
	GetWindowText (strCaption);

	if(!strCaption.IsEmpty()) {
		if((GetStyle() & BS_MULTILINE) == BS_MULTILINE) {
			dwFormat &= ~DT_SINGLELINE;
			dwFormat &= ~DT_VCENTER;
			dwFormat |= DT_WORDBREAK;

			RECT rct = rectCaption;
			int height = ::DrawText(pDC->m_hDC, (LPCTSTR) strCaption, strCaption.GetLength(), &rct, DT_CALCRECT | DT_WORDBREAK);
			rectCaption.top = rectCaption.CenterPoint().y – height / 2;

			::DrawTextEx (pDC->m_hDC, strCaption.GetBuffer(0), strCaption.GetLength(), &rectCaption, dwFormat, NULL);
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Categories

April 2009
M T W T F S S
« Dec   May »
 12345
6789101112
13141516171819
20212223242526
27282930  

Blog Stats

  • 27,491 hits
%d bloggers like this: