Last Modification: December 11, 1999

How do I change the font of a dialog control?

You can set the font for any of the standard Windows controls by using the WM_SETFONT message, which maps to CWnd::SetFont() in MFC. The following code example illustrates how you can use this to set the font of a control that's been added to a dialog dynamically:


BOOL CMyDialog::OnInitDialog()
{
	...

	/* Get the size of a standard button */
	CRect rectButton;
	HWND hWnd = ::GetDlgItem(m_hWnd, IDOK);
	::GetWindowRect(hWnd, rectButton);
	ScreenToClient( rectButton );

	/* Calculate a position for the new button */
	const int width = rectButton.Width();
	rectButton.left = 100;
	rectButton.right = rectButton.left + width;

	/* Create the new button.
	 * (m_btn is a CButton member variable of the dialog class)
	 */
	m_btn.Create( "My Button", WS_CHILD | WS_VISIBLE,
			rectButton, this, ID_MY_BUTTON );

	/* Ensure that the new button uses the same
	 * font as the standard buttons on the dialog
	 */
	HFONT hFont = ::SendMessage( hWnd, WM_GETFONT, 0, 0 );
	::SendMessage( m_btnPreview.m_hWnd, WM_SETFONT, (WPARAM) hFont,
			MAKELPARAM(false, 0) );
	...
}

References  and Samples: