News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

QA: How to emulate DT_END_ELLIPSIS flag of DrawText function?

By Alexander Shargin, May 27, 2002.
Print version

Question

On desktop platforms DrawText function supports DT_END_ELLIPSIS flag which allows to truncate the string and add ellipsis ("...") to the end of it if it doesn't fit in the specified rect. But this flag is not implemented on Pocket PC. How can I emulate it?

Answer

The main idea is to use CDC::GetTextExtent method to determine text's width. If it's greater than the width of the rect we can truncate the string, append ellipsis to the end of it, call CDC::GetTextExtent method again and repeat this procedure in a loop till the string fits in the rect. Then we can call DrawText to display the string.

Here is a sample implementation of DrawTextEndEllipsis function which behaves like desktop version of DrawText with DT_END_ELLIPSIS flag set.

int DrawTextEndEllipsis(CDC &dc, const CString &strText, CRect rc, UINT uFormat) { int nWidth = rc.Width(); if(dc.GetTextExtent(strText).cx > nWidth) { // Text doesn't fit in rect. We have to truncate it and add ellipsis to the end. CString strTemp = strText; for(int i=strText.GetLength(); i>=0; i--) { strTemp = strText.Left(i) + _T("..."); if(dc.GetTextExtent(strTemp).cx < nWidth) { // Gotcha! break; } } return dc.DrawText(strTemp, rc, uFormat); } return dc.DrawText(strText, rc, uFormat); }

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House