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

x
?
Solved

bad return value for GetTextExtent

Posted on 2005-03-08
5
Medium Priority
?
565 Views
Last Modified: 2013-11-20
Hi,

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 ?
0
Comment
Question by:__Julien__
  • 3
5 Comments
 
LVL 48

Accepted Solution

by:
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.
0
 

Author Comment

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

Author Comment

by:__Julien__
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):

      HRESULT OnDraw(ATL_DRAWINFO& di)
      {
            CDC context;
            context.SetOutputDC( di.hdcDraw );
                ...

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

Author Comment

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

Expert Comment

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

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