Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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?
0
pcommons
Asked:
pcommons
1 Solution
 
l3st4rdCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now