# Get Font from CDC or DC

This topic is 4130 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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632619
• Total Posts
3007496
• ### Who's Online (See full list)

There are no registered users currently online

×