# Get Font from CDC or DC

This topic is 3918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, I got a CDC or a DC and I wish to get the Font properties used with this device contex, espacialy I want to get the width and the height of the font :) Thanks in advance!

##### Share on other sites
If you have a CDC then you can call GetCurrentFont() to retrieve a CFont.

If you have a DC then you can call GetCurrentObject().

##### Share on other sites
GetCurrentObject will return you the current font handle. If you actually want info about the font then you pass the handle in turn to GetObject. That will get you back a LOGFONT that you can look at.

Or, depending on what exactly it is you want to do, you can skip all that and just call one of the GetTextExtent functions.

##### Share on other sites
I want to calculate a width and the height of a string created by that font.

##### Share on other sites
That is precisely what GetTextExtentPoint is for.

##### Share on other sites
Thanks a lot ! :)

##### Share on other sites
I"m still having trouble with getting the Font properties and calculating the text width of a DC object :/

When I use the LOGFONT information I've retrived using the DC I'm ALWAYS getting
lfFaceName : System
lfWidth: 7
lfHeight: 16

But when I use the LOGFONT information I"ve got from the CWnd::GetFont I get the correct Font FaceName but The width and the height parametrs are not being retrived, this is what i get:
lfFaceName : correct face name
lfWidth: 0 ( Always )
lfHeight: -11 or -13

Here is what I'm doing:
This is a OnHotKey function inside my dialog (CDialog derived)
{ POINT p; GetCursorPos(&p);  CWnd *wnd = WindowFromPoint(p); CDC *dc = wnd->GetDC();   //retrive LOGFONT information using DCif(dc){CFont *f = dc->GetCurrentFont(); if(f) {	LOGFONT lf;	f->GetLogFont(&lf);	CString s;s.Format(L"FaceName[%s] W[%d] H[%d]",lf.lfFaceName,lf.lfWidth,lf.lfHeight);   AfxMessageBox(s); }}// retrive LOGFONT information using CWndCFont *font = wnd->GetFont();if(font!=NULL){CString s;font->GetLogFont(&lf);s.Format("FaceName[%s] W[%d] H[%d]\r\n"),lf.lfFaceName,lf.lfWidth,lf.lfHeight);AfxMessageBox(s);} }

##### Share on other sites
A width of 0 and a negative height are valid. They both mean, basically, that the width and height are not determined yet - they are determined exactly by the system when rendering the text and tell the system to find a best match set of dimensions for the device.

##### Share on other sites
Quote:
 Original post by Colin JeanneA width of 0 and a negative height are valid. They both mean, basically, that the width and height are not determined yet - they are determined exactly by the system when rendering the text and tell the system to find a best match set of dimensions for the device.

So how can I use it properly?

##### Share on other sites
Quote:
Original post by spree
Quote:
 Original post by Colin JeanneA width of 0 and a negative height are valid. They both mean, basically, that the width and height are not determined yet - they are determined exactly by the system when rendering the text and tell the system to find a best match set of dimensions for the device.

So how can I use it properly?

By using the GetTextExtentPoint function you were told about a few posts up. Remember that most fonts have variable width characters, so just attempting to multiply a single width with the number of characters will surely fail.

##### Share on other sites
Quote:
Original post by wack
Quote:
Original post by spree
Quote:
 Original post by Colin JeanneA width of 0 and a negative height are valid. They both mean, basically, that the width and height are not determined yet - they are determined exactly by the system when rendering the text and tell the system to find a best match set of dimensions for the device.

So how can I use it properly?

By using the GetTextExtentPoint function you were told about a few posts up. Remember that most fonts have variable width characters, so just attempting to multiply a single width with the number of characters will surely fail.

But as Colin Jeanne Said, that the negative lendgth and width values I receive say that "the width and height are not determined yet" will this prevent GetTextExtentPoint from evaluating the correct width and height values?

##### Share on other sites
No, because GetTextExtentPoint and GetTextExtentPoint32 (which is what you should use instead), evaluates the string that you're about to display.

##### Share on other sites
Quote:
 Original post by LessBreadNo, because GetTextExtentPoint and GetTextExtentPoint32 (which is what you should use instead), evaluates the string that you're about to display.

Thats the thing, I"m trying to evaluate strings which are ALREADY displayed.
Using the HotKey I"ve registered I"m getting the mouse cursors position and from this position I"m getting my DC and the Text I'm getting using Active Accessibilty.

So to be able to pin point the word I'm pointing at with the cursor I need the DC->GetTextExtentPoint32 to calcualte the Font which was used Originaly to display this text.

##### Share on other sites
What does handling hotkeys have to do with tracking already displayed strings in a dialog? Isn't that more about managing the keyboard? Are you trying to rig up some kind of text selection functionality?

Maybe this will help: How to retrieve text under the cursor (mouse pointer)

##### Share on other sites
Quote:
 Original post by LessBreadWhat does handling hotkeys have to do with tracking already displayed strings in a dialog? Isn't that more about managing the keyboard? Are you trying to rig up some kind of text selection functionality? Maybe this will help: How to retrieve text under the cursor (mouse pointer)

I"m trying to build a Screen Reader with the main functionality that I can detect a specific word under the mouse's cursor. The problem I have with Active Accessibility is that it returns the whole active accessibility object where it contains a centence with one of it's word is the word under the cursor that i need. For me to be able to exclude this word from the rest of the sentence I need to know the length and the height properties of this active accessibilty object.

• 11
• 16
• 11
• 13
• 11