Solved

Calculating String's Exact width and height

Posted on 2004-09-20
2
287 Views
Last Modified: 2013-11-20
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
Comment
Question by:pcommons
2 Comments
 

Accepted Solution

by:
l3st4rd earned 50 total points
ID: 12107764
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
 

Author Comment

by:pcommons
ID: 12117050
Thanks. I was looking at CFont methods and didn't think to look at CDC methods.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prevent this page from creating additional dialogs. 3 534
while loop over for loop 7 104
Problem to event 3 97
how to split multiple lines delimiter : 8 99
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question