Solved

Calculating String's Exact width and height

Posted on 2004-09-20
2
285 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now