Link to home
Start Free TrialLog in
Avatar of ames
ames

asked on

How to get font size of your system?

Any API call can give me the font size of my system? Font size you get when you click right mouse button on your screen and then select properties and then select setting tab.
ASKER CERTIFIED SOLUTION
Avatar of psdavis
psdavis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jstolan
jstolan

I don't believe psdavis is correct.  No GetDeviceCaps function will tell you the system font size.  One way to do it is to get a screen compatible display context using CreateDC.  Then get the system font using GetStockObject(SYSTEMFONT).  Load the font into the CDC using SelectObject.  Finally, you can use the GetTextMetrics call to determine any information you need, including the size.  Don't forget to free up everything correctly.
The settings you need are stored in the registry:

1. Fetch the current configuration profile, stored here:

HKLM\System\CurrentControlSet\IDConfigDB\CurrentConfig

The value you get there is something like '0001'.

2. Using this value, the display dpi settings are stored here:

HKLM\Config\[ccfg]\Display\Settings
// [ccfg] <-- contains the value from [1]

The value names are:
DPILogicalX, DPILogicalY and
DPIPhysicalX, DPIPhysicalY

But i believe that psdavis is right. The way he proposed should deliver correct values, and
it doesn't depend on the registry.
BTW: The dpi settings *do* contain the system font size - as they tell how to scale the fonts.
Thanks snoeg!
psdavis is correct. A complete version is:

CDC DispDC;
DispDC.CreateIC("DISPLAY", NULL, NULL, NULL);
int nLogDPIX = DispDC.GetDeviceCaps(LOGPIXELSX),
    nLogDPIY = DispDC.GetDeviceCaps(LOGPIXELSY);

// if (nLogDPIX == 96 && nLogDPIY == 96) // small fonts
// if (nLogDPIX == 120 && nLogDPIY == 120) // large fonts
// otherwise, custom font size

Well there's an easier way to get all font attributes

LOGFONT lf ;
GetObject( GetStockObject(SYSTEM_FONT), sizeof lf, &lf ) ;

*All* the font attributes are then in lf.
Sorry that was SDK, add casts and ::
Avatar of ames

ASKER

psdavis was right. Snoegler's answer with registry works only for 95. For NT the entries are in different location. jstolan's answer may work, but I did not try it. Chensu, as always, makes it easier by providing a complete version. Thanks to you all.
Avatar of ames

ASKER

PSDavis was right. Chensu, as always, makes it easier by providing complete solution. I did not try Jstolan's solution. Snoegler's solution works for 95. It means that you need to check for OS and then read different registry location based on OS. I used Chensu's solution and it works fine. Thanks to you all.