How to Simulate Changing the Font in a Message Box

Last reviewed: November 2, 1995
Article ID: Q68586
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 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
    

SUMMARY

To simulate changing the font in a message box, create a dialog box that uses the desired font. Specify the style and contents of the dialog box to reflect the style of the desired message box. The application can also draw a system icon in the dialog box.

MORE INFORMATION

The message box is a unique object in Windows. Its handle is not available to an application; therefore, it cannot be modified. An application can simulate a message box with a different font by creating a dialog box that looks like a message box.

To change the font in a dialog box, use the optional statement FONT in the dialog statement of the resource script (.RC) file. For example, resource file statements for a dialog box displaying an error in Courier point size 12 would be as follows:

   FontError DIALOG  45, 17, 143, 46
   CAPTION "Font Error"
   FONT 12, "Courier"
   STYLE WS_CAPTION | WS_SYSMENU | DS_MODALFRAME
   BEGIN
       CTEXT "Please select the right font", -1, 0, 7, 143, 9
       DEFPUSHBUTTON "OK"  IDOK,   56, 25,  32, 14, WS_GROUP
   END

To center the dialog box in the screen, use GetWindowRect() to retrieve the dimensions of the screen and MoveWindow() to place the dialog box appropriately. The following code demonstrates this procedure:

   case WM_INITDIALOG:
       GetWindowRect(hDlg, &rc);
       x = GetSystemMetrics(SM_CXSCREEN);
       y = GetSystemMetrics(SM_CYSCREEN);
       MoveWindow (hDlg,
          (x - (rc.right - rc.left)) >> 1,  /* x position */
          (y - (rc.bottom - rc.top)) >> 1,  /* y position */
          rc.right - rc.left,               /* x size */
          rc.bottom - rc.top,               /* y size */
          TRUE);                            /* paint the window */
       return TRUE;

To display a system icon in the dialog box, call the DrawIcon() function during the processing of a WM_PAINT message. After drawing the desired icon, the dialog procedure passes control back to the dialog manager by returning FALSE. The code to paint the exclamation point icon (used in warning messages) is as follows:

   case WM_PAINT:
       hIcon = LoadIcon(NULL, IDI_EXCLAMATION);
       hDC = GetDC(hDlg);
       DrawIcon(hDC, 20, 40, hIcon);
       ReleaseDC(hDlg, hDC);
       return FALSE;


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


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.