Draw single line text in a rect

  • Thread starter Thread starter Kay L.
  • Start date Start date
K

Kay L.

Hi, when I draw a long text inside a rect, it will go outside, I want to
know which charactor is the last one which is inside the rect. I know I can
do like below, but it's not efficient, is there any better way to do this,
thanks.

CString str = "a long text"; // if it's too long, I want to draw "a long
t...";
CRect rc(0,0,100,100);
char c;
for(int i=0; i<str.GetLength(); i++)
{
CSize sz = pDC->GetTextExtent(0,0,str);
if(sz.cy > rc.Width())
{
c = str.GetAt(i);
break;
}
}
 
Hi Kay,

For a start the code you posted won't work (or even compile I think).
Assuming that is just a typo then if you actually need to know the character
count that fits, you will need to do something along those lines. If you
just need to draw as you described then look up DrawText and DT_END_ELLIPSIS

Cheers

Doug Forster
 
Thank you very much, Doug, "DT_END_ELLIPSIS" is exactly what I want.

Cheers!

Kay

"Doug Forster" <doug_ZAPTHIS_AT_TONIQ_ZAPTHIS_co.nz> wrote in message
Hi Kay,

For a start the code you posted won't work (or even compile I think).
Assuming that is just a typo then if you actually need to know the character
count that fits, you will need to do something along those lines. If you
just need to draw as you described then look up DrawText and DT_END_ELLIPSIS

Cheers

Doug Forster
 
Back
Top