WM_SETFONT Message

An application sends a WM_SETFONT message to specify the font that a control is to use when drawing text.

Syntax

To send this message, call the SendMessage function as follows.
lResult = SendMessage(    // returns LRESULT in lResult
   hWndControl,           // (HWND) handle to destination control
   WM_SETFONT,            // (UINT) message ID
   wParam,                // = () wParam; 
   lParam                 // = () lParam;
);

Parameters

wParam
Handle to the font (HFONT). If this parameter is NULL, the control uses the default system font to draw text.
lParam
The low-order word of lParam specifies whether the control should be redrawn immediately upon setting the font. If this parameter is TRUE, the control redraws itself.

Return Value

This message does not return a value.

Remarks

The WM_SETFONT message applies to all controls, not just those in dialog boxes.

The best time for the owner of a dialog box control to set the font of the control is when it receives the WM_INITDIALOG message. The application should call the DeleteObject function to delete the font when it is no longer needed; for example, after it destroys the control.

The size of the control does not change as a result of receiving this message. To avoid clipping text that does not fit within the boundaries of the control, the application should correct the size of the control window before it sets the font.

When a dialog box uses the DS_SETFONT style to set the text in its controls, the system sends the WM_SETFONT message to the dialog box procedure before it creates the controls. An application can create a dialog box that contains the DS_SETFONT style by calling any of the following functions:

Message Information

HeaderDeclared in Winuser.h, include Windows.h
Minimum operating systems Windows 95, Windows NT 3.1

See Also

Tags :


Community Content

Đonny
Value
WM_SETFONT = &H30
Tags : value constant

Simón Medrano
Sample code for changing a font used for text in a control from the default font.

This material does provide the answer. However I thought that a very simple piece of sample code would help others understand how to change a default font on a control.

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
120, 40, 400, 400, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
// Set the font for all buttons etc:
hFont=CreateFont (14, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, \
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
DEFAULT_PITCH | FF_SWISS, L"Arial");
// Create our control:
hBtnButton1 = CreateWindow (L"BUTTON", L"Button1", \
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER, \
10, 10, 50, 25, hWnd, (HMENU) IDR_BUTTON1, hInst, NULL);
if (hBtnButton1 == NULL)
{
MessageBox (hWnd, L"hBtnButton1 not created", L"Create hBtnButton1", IDOK);
}
// Set the new font for the control:
SendMessage (hBtnButton1, WM_SETFONT, WPARAM (hFont), TRUE);
  ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}

Kenneth Spencer


Mike Dimmick
Font object lifetime

The font object whose handle is passed in the wParam parameter must live at least as long as the control that is sent the message does. You must not delete this font until either a) you destroy the control, or b) set a different font by sending WM_SETFONT again.

The control will use this handle directly in its WM_PAINT handling (by passing it to SelectObject) and strange behaviour will occur if the object has been deleted. If the handle has been reused you might get a different font, or it might point to a different type of GDI object altogether, causing other painting errors.

It is permissible to use the same font object with multiple controls, as long as the font object's lifetime is at least as long as the longest-lived control. You could, for example, create the font object during program startup and retain it for the full lifetime of the program.

Tags :

Page view tracker