NO

Author Topic: Default Windows font  (Read 847 times)

Offline henrin

  • Member
  • *
  • Posts: 21
Default Windows font
« on: October 19, 2015, 02:05:54 pm »
Any program should conform the Windows settings. When using menu or message boxes, that is automatic. But if a want to use the same font is own drawn objects, I need to get the font description.

I have found a solution in the registry. HKCU\Control Panel\Desktop\WindowMetrics\ contains binary data for each font, and this data corresponds to the LOGFONTW structure. Do you think it is safe?

Any way, I now use another method: the SystemParametersInfo() function. For older Windows versions, the SPI_GETNONCLIENTMETRICS structure is smaller (read carefully the Microsoft documentation!). One should check the Windows version at runtime (not at compile time). As I am not sure of the exact version numbers, I try both cases :


void defaultFontGet(LOGFONT *defFont)       /* get the Windows menu font */
{
    NONCLIENTMETRICS ncm ;
    LOGFONT *fnt = &(ncm.lfMenuFont) ;

    ncm.cbSize = sizeof(ncm) ;
    int e = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0) ;

    if (e == 0)        /* for old Windows version - cf Microsoft documentation  */
    {
        ncm.cbSize -= 4 ;
        e = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0) ;
    }

    if (e == 0)                 /* if something is still wrong                  */
    {
        memset((void*) fnt, 0, sizeof(LOGFONT)) ;    /* defaults    values      */
        fnt->lfHeight = - 11 ;
        fnt->lfCharSet = DEFAULT_CHARSET ;
        fnt->lfWeight = FW_NORMAL ;
        strcpy(fnt->lfFaceName, "Tahoma") ;
    }

    memcpy(defFont, fnt, sizeof(LOGFONT)) ;
}