Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


bad return value for GetTextExtent

Posted on 2005-03-08
Medium Priority
Last Modified: 2013-11-20

I can't get CDC::GetTextExtent to return correct values.
Here's my code:

        LOGFONT lf;  
        ZeroMemory(&lf, sizeof(LOGFONT));
        wcscpy(lf.lfFaceName, _T("Arial Black") );
        lf.lfHeight = 14;
      CFont font;
        font.CreateFontIndirect( &lf );
      context.SelectObject( font );
      CSize size = context.GetTextExtent( _T("Test") );

The value of size is not correct. It doesn't even depend on the value of the input string.

What's wrong ?
Question by:__Julien__
  • 3
LVL 48

Accepted Solution

AlexFM earned 150 total points
ID: 13487230
context.SelectObject( font );

Replace with:

context.SelectObject( &font );

Test values returned by CreateFontIndirect and SelectObject to see what is the problem.

Author Comment

ID: 13487333
That was indeed an error but I've still the same problem after correction.

Author Comment

ID: 13496318
I've tried the previous code in a new dialog box and it worked. So, I guess the problem lies in the device context. I created it like this (it's in an ActiveX control):

            CDC context;
            context.SetOutputDC( di.hdcDraw );

It worked well for drawing lines and rectangles but not for this. Why?

Author Comment

ID: 13496622
With Attach instead of SetOutputDC, it works fine.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13503974
try to use context.SetMapMode(MM_TEXT); before calling GetTextExtent().

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

578 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