Calculating String's Exact width and height

Is there a quick and easy way, if I have a CFont structure and a CString to figure out exactly how tall and wide the string would be when rendered using that CFont info?
pcommonsAsked:
Who is Participating?
 
l3st4rdConnect With a Mentor Commented:
you can only get the size that a given string would take with a given font on a given device context.
thus you need the CDC object into which your CFont has been selected. (a pointer to a windows CDC object can be obtained by calling CWND::GetDC() . )
now u simply need to call the CDC's GetTextExtend(...) member.
like:

CFont myFont,
         *pOldFont;
CString myText;
myText = "hello";

//...
//initialize your font here
//...

CDC *pDC = GetDC(); //get a pointer to the device context
pOldFont = pDC->SelectObject(&myFont);      //only needed if font has not already been selected to device context

//the size is querried here:
CSize size = pDC->GetTextExtend(myText);

pDC->SelectObject(pOldFont);                       //again only needed if font was not selected before

daniel.
0
 
pcommonsAuthor Commented:
Thanks. I was looking at CFont methods and didn't think to look at CDC methods.
0
All Courses

From novice to tech pro — start learning today.