CHARFORMAT to LOGFONT



Thought someone might require this conversion.

CharformatToLogfont(CHARFORMAT & cf, LOGFONT & lf, COLORREF & cr)
{
 
	lf.lfCharSet = cf.bCharSet;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfQuality = DEFAULT_QUALITY;
	lf.lfPitchAndFamily = DEFAULT_PITCH;
 
 
	if ( (cf.dwEffects & CFE_BOLD) == CFE_BOLD)
	{
		lf.lfWeight = FW_BOLD;
	}
 
	CDC dc;
	dc.CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
	lf.lfHeight = -MulDiv(cf.yHeight/20, dc.GetDeviceCaps(LOGPIXELSY), 72);
	dc.DeleteDC();
 
	lf.lfUnderline = ( (cf.dwEffects & CFE_UNDERLINE) == CFE_UNDERLINE);
	lf.lfStrikeOut = ( (cf.dwEffects & CFE_STRIKEOUT) == CFE_STRIKEOUT);
	lf.lfItalic = ( (cf.dwEffects & CFE_ITALIC) == CFE_ITALIC);
 
	lf.lfWidth = 0;
	_tcscpy_s(lf.lfFaceName, LF_FACESIZE, cf.szFaceName);
 
	//save color separately because LOGFONT does not accept color
	cr = cf.crTextColor;
 
}


Leave a Reply

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