Sign in to follow this  

Get Font from CDC or DC

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 DC
if(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 CWnd
CFont *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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Colin Jeanne
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.


So how can I use it properly?

Share this post


Link to post
Share on other sites
Quote:
Original post by spree
Quote:
Original post by Colin Jeanne
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by wack
Quote:
Original post by spree
Quote:
Original post by Colin Jeanne
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by LessBread
No, 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 this post


Link to post
Share on other sites
Quote:
Original post by LessBread
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)


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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this