Wednesday, March 25, 2009

Use Bold or Underlined Font

1. Define a static HFONT variable:

static HFONT hFont;

2. In the response to the WM_INITDIALOG message, create a font with desired features, using CreateFont function:

HFONT CreateFont(
int nHeight, // logical height of font
int nWidth, // logical average character width
int nEscapement, // angle of escapement
int nOrientation, // base-line orientation angle
int fnWeight, // font weight
DWORD fdwItalic, // italic attribute flag
DWORD fdwUnderline, // underline attribute flag
DWORD fdwStrikeOut, // strikeout attribute flag
DWORD fdwCharSet, // character set identifier
DWORD fdwOutputPrecision, // output precision
DWORD fdwClipPrecision, // clipping precision
DWORD fdwQuality, // output quality
DWORD fdwPitchAndFamily, // pitch and family
LPCTSTR lpszFace // pointer to typeface name string
);


Use the pre-defined FW_BOLD parameter for fnWeight for Bold text.
Set fdwUnderline parameter to TRUE for underlined text.

Use default values for most of the other parameters.

Here is a simple helper function:

HFONT SimpleCreateFont( int Height, BOOL Bold, BOOL Italic, BOOL Underline,
BOOL StrikeOut, DWORD Family, char* FaceName )
{
HFONT Ret;
int Weight;
Weight = Bold ? FW_BOLD : FW_NORMAL;
Ret = CreateFont( Height, 0, 0, 0, Weight,
Italic, Underline, StrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | Family, FaceName );
return( Ret );
}


3. Use the WM_SETFONT message to tell child control to use new font

SendMessage( GetDlgItem( hwnd, ID_WEB ), WM_SETFONT, (WPARAM)hFont, 0 );

No comments:

Post a Comment