Jump to content
  • Advertisement
Sign in to follow this  
spree

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.

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
Advertisement
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 want to calculate a width and the height of a string created by that font.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!