Thursday, September 11, 2008

Draw Multi-line Text into a Window in MFC

// 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;

}

}

}

}

No comments: