INF: How to Simulate Changing the Font in a Message Box

ID Number: Q68586

3.00

WINDOWS

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.0