Changing the Font Used by Dialog Controls in Windows

Last reviewed: November 2, 1995
Article ID: Q74737
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

In Windows 3.x, there are two ways to specify the font used by dialog controls:

  1. The FONT statement can be used in the dialog template to specify the font used by ALL the controls in the dialog box.

    -or-

  2. The WM_SETFONT message can be sent to one or more dialog controls during the processing of the WM_INITDIALOG message.

If a font is specified in the dialog template, the controls will use a bold version of that font. The following code demonstrates how to change the font used by dialog box controls to a nonbold font using WM_SETFONT. The font should be deleted with DeleteObject() when the dialog box is closed.

HWND hDlg; HFONT hDlgFont; LOGFONT lFont;

case WM_INITDIALOG:

    /* Get dialog font and create non-bold version */
    hDlgFont = NULL;
    if ((hDlgFont = (HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0L))
             != NULL)
       {
       if (GetObject(hDlgFont, sizeof(LOGFONT), (LPSTR)&lFont)
                 != NULL)
          {
          lFont.lfWeight = FW_NORMAL;
          if ((hDlgFont = CreateFontIndirect(&lFont)) != NULL)
             {
             SendDlgItemMessage(hDlg, CTR1, WM_SETFONT, hDlgFont, 0L);
             // Send WM_SETFONT message to desired controls
             }
          }
       }
    else  // user did not specify a font in the dialog template
       {  // must simulate system font
       lFont.lfHeight = 13;
       lFont.lfWidth = 0;
       lFont.lfEscapement = 0;
       lFont.lfOrientation = 0;
       lFont.lfWeight = 200; // non-bold font weight
       lFont.lfItalic = 0;
       lFont.lfUnderline = 0;
       lFont.lfStrikeOut = 0;
       lFont.lfCharSet = ANSI_CHARSET;
       lFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
       lFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
       lFont.lfQuality = DEFAULT_QUALITY;
       lFont.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
       lFont.lfFaceName[0] = NULL;
       hDlgFont = CreateFontIndirect(&lFont);

       SendDlgItemMessage(hDlg, CTR1, WM_SETFONT, hDlgFont,
          (DWORD)TRUE);
       // Send WM_SETFONT message to desired controls
       }

   return TRUE;
   break;


Additional reference words: 3.00 3.10 3.50 4.00 95
KBCategory: kbui
KBSubcategory: UsrDlgs


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.