Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • 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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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