LOGFONT to CHARFORMAT



Thought someone might require this conversion.

 
LogfontToCharformat(LOGFONT & lf, COLORREF & cr, CHARFORMAT & cf)
{
 
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_CHARSET 
        | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_OFFSET;
	cf.dwEffects = 0;
 
	if (lf.lfWeight >= FW_BOLD)
	{
		cf.dwEffects |= CFE_BOLD;
	}
 
	if (lf.lfUnderline)
	{
		cf.dwEffects |= CFE_UNDERLINE;
	}
 
	if (lf.lfItalic)
	{
		cf.dwEffects |= CFE_ITALIC;
	}
 
	if (lf.lfStrikeOut)
	{
		cf.dwEffects |= CFE_STRIKEOUT;
	}
 
	//temporary create DC
	CDC dc;
	dc.CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
	cf.yHeight = 20*long( 0.5 + fabs(double(72*lf.lfHeight)/dc.GetDeviceCaps(LOGPIXELSY)));
	dc.DeleteDC();
 
	cf.yOffset = 0;
	cf.crTextColor = cr;
	cf.bCharSet = lf.lfCharSet;
	cf.bPitchAndFamily = lf.lfPitchAndFamily;
	_tcscpy_s(cf.szFaceName, LF_FACESIZE, lf.lfFaceName);
}


Leave a Reply

Your email address will not be published. Required fields are marked *