A Journey 2 Eternity

Draw Multi-line Text into a Window in MFC

Posted on: September 11, 2008

/*
 * Draw multiline text into a window
 *
 * pDC : Specifies the device context
 * str : Specifies the string
 * rectText : Specifies the text rectangle
 * nFirstVisibleLineIndex : Specifies the first visible line index
 * nFormat : Specifies the method of formatting the text
 */

void CEMCHotCtrl::DrawMultilineText(CDC* pDC, CString str, CRect rectText, int nFirstVisibleLineIndex, int nFormat)
{
	if(str.GetLength() > 0) {
		const TCHAR* p = (LPCTSTR) str;
		CSize sz = pDC->GetTextExtent(p);
		const TCHAR* end = p;
		int nLine = 0;

		if(nFirstVisibleLineIndex > 0) {
			while(*end != 0) {
				if(*end == _T(‘\n’)) {
					nLine++;
					if(nLine == nFirstVisibleLineIndex) {
						end++;
						break;
					}
				}
				end++;
			}
		}

		int len = 0;
		TCHAR buf[10000];

		while(*end != 0) {
			buf[len] = *end;
			end++;
			len++;

			if(*end == _T(‘\n’) || (*end == 0)) {
				if(buf[len - 1] == _T(‘\r’)) {
					buf[len - 1] = 0;
				}

				buf[len] = 0;
				pDC->DrawText(buf, -1, rectText, nFormat);

				if(*end == _T(‘\n’)) {
					end++;
				}

				rectText.top += sz.cy;
				len = 0;
			}
		}
	}
}
Advertisements

1 Response to "Draw Multi-line Text into a Window in MFC"

good job

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

September 2008
M T W T F S S
« Aug   Dec »
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 26,968 hits
%d bloggers like this: