Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CDC: Text Width & Height

Posted on 2009-06-30
7
Medium Priority
?
825 Views
Last Modified: 2012-06-27
I need a way to calculate the actual height of a character drawn with CDC::DrawText.

Take for example the attached picture where the letter "s" is drawn with a grey background.
GetTextExtend returns the height of the character including this background space.
Same goes for DrawText if used with the DT_CALCRECT flag.

Is there a way to get the height of only the actual character?
Just from the top of the "s" to the bottom of it ?

Thanks..







char.JPG
0
Comment
Question by:Invader
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 120 total points
ID: 24744833
Hi Invader,

I'm not sure if it works, but you can try following:

- start creating a path for the DC using BeginPath.
- draw the text with DrawText.
- call EndPath to stop creating the path.
- create a region from the path using PathToRegion.
- use GetRgnBox to get the bounding box of the create region.

Another method (which I even don't know if it works) could be:

- create a memory device context
- use SetBoundsRect, DrawText and GetBoundsRect to get the dimensions.

For both a path and the boundsrect I'm not sure if really only the drawn part of the character is used, therefor I fear you have to test them.

If none of them works you can even try to get the real size of the character using GetGlyphOutline with GGO_METRICS - I never used this before, but if I understand correctly what it does it should return the info you need in a GLYPHMETRICS structure ...


Hope that helps,

ZOPPO
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 120 total points
ID: 24745252
Use the function GetTextExtentPoint32 like

  SIZE siz = { 0 };
  GetTextExtentPoint32(hdc, "s", 1, &siz);

Note, the hdc must be a handle to the device context actually used for drawing the text. So, you best calculate the size when you actually were drawing in one of the drawing handler functions called after a WM_PAINT message.  

>>>> For both a path and the boundsrect I'm not sure if really only the drawn part of the character is used, therefor I fear you have to test them.
Indeed, I share the doubts with Zoppo. So, the width of the "s" string probably is ok, but probably the height will contain some 'internal leading' which is immanent to the font but not to the character 's'.
0
 

Author Comment

by:Invader
ID: 24745421
Zoppo:

I tried both the BeginPath and the SetBoundsRect approach. They work as they should but they still include the background area of the font.

I also did some testing with GetGlyphOutline with no luck but I will do some more. I think this one is the most likely to produce the desired result since its supposed to return the outline of the string.

itsmeandnobodyelse:

GetTextExtentPoint32 is the same as GetTextExtend. If you follow the latter, it calls the first one. However, you are right, the width is correct for any string and the height returned is every time of "fixed" size as required by the selected font.

------
I think best bet, is GetGlyphOutline...if I manage to get it to work..
Although it still has the drawback that it works only for True Type Fonts.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Expert Comment

by:Zoppo
ID: 24745692
> works only for True Type Fonts
I'm not sure, it maybe it works for none-TrueType vector fonts too (AFAIK windows FNT-files can contain raster- or vector fonts).

For raster fonts I guess there's no possibility to get the info you want except maybe analyse the bitmap stored in the FNT file for the character to find the bounding of none-white pixels, since a raster font is just a bunch of bitmaps of same heigth.

ZOPPO
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24746101
>>>> except maybe analyse the bitmap stored in the FNT file for the character
Maybe it is easier to read the bits of the rectangle on screen into a bitmap. If the background was white or unicolored, it shouldn't be much difficult to find the first non-white pixel when going from top to bottom for all columns.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 24746181
... or analyze the bitmap returned by GetGlyphOutline using GGO_BITMAP.
0
 

Accepted Solution

by:
Invader earned 0 total points
ID: 24767872
Apparenty GetGlyphOutline just returns the points of the outline that is stored in the font.
That is, regardless of font selected.
The font contains the outline, and it is transformed in other sizes when drawn..

I think Im giving up on this..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

876 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