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__
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

800 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