VisualC++ Tips

Just another WordPress.com weblog

CDC::DrawText

The syntax

int DrawText( const CString& str, LPRECT lpRect, UINT nFormat );

We dont know the width and height of the rectangle where the text to display, so that the lpRect is empty
Then how calculate the text area ?

It is simple,

Eg:

CRect rcText( 0, 0, 0 ,0 );

dc.DrawText( csText, &rcText, DT_CALCRECT );
dc.DrawText( csText, &rcText, DT_CENTER );

The first DrawText fills rcText with required value, ie the hight of the text, width of the text.
Call next DrawText, then the text will display.

The return of the DrawText is the height of the text. But we also have to calculate the width of the text. So the above method is easy to use.

July 10, 2008 - Posted by afsalm | Uncategorized | | No Comments Yet

No comments yet.

Leave a comment