WM_SETFONT message
Applies to: desktop apps only
Sets the font that a control is to use when drawing text.
#define WM_SETFONT 0x0030
Parameters
- wParam
-
A 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
Type: LRESULT
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:
Requirements
|
Minimum supported client | Windows 2000 Professional |
|---|---|
|
Minimum supported server | Windows 2000 Server |
|
Header |
|
See also
- Reference
- CreateDialogIndirect
- CreateDialogIndirectParam
- DialogBoxIndirect
- DialogBoxIndirectParam
- DLGTEMPLATE
- MAKELPARAM
- WM_GETFONT
- WM_INITDIALOG
- Conceptual
- Windows
- Other Resources
- DeleteObject
Send comments about this topic to Microsoft
Build date: 2/3/2012
Windows will a default margin on edit controls based on the font size - the larger the font, the bigger the margin.
- 3/16/2010
- mogens
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.
- 5/28/2008
- Mike Dimmick
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
- 2/25/2008
- kaspencer
- 4/28/2008
- Simón Medrano