Get Font from CDC or DC

Started by
13 comments, last by spree 16 years, 10 months ago
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!
;)
Advertisement
If you have a CDC then you can call GetCurrentFont() to retrieve a CFont.

If you have a DC then you can call GetCurrentObject().
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.
-Mike
I want to calculate a width and the height of a string created by that font.
;)
That is precisely what GetTextExtentPoint is for.
-Mike
Thanks a lot ! :)
;)
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);} } 


;)
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.
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?
;)
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.

This topic is closed to new replies.

Advertisement