Click to See Complete Forum and Search --> : Using CFontDialog to change font on CRichEditCtrl


Brad Younie
April 8th, 1999, 04:22 PM
I'm writing an app that uses a CRichEditCtrl. I want to be able to use a CFontDialog to let the users change the Rich control's font. The problem is that the Rich controls use a CHARFORMAT to hold the font data and CFontDialogs use CHOOSEFONT.

Can anyone shed some light on this?

ashes jingles
May 15th, 1999, 06:19 AM
some light!! I will give you the whole sun , but it's a hack.
if you look at the MFC code , CFontdialog has a constructor that will take CHARFORMAT( this fact is undocumented , though..) as it's paramater , though I dont remember how you'll get it back , so just see the source.
hope that helps
jingles

mfcUser
May 8th, 2000, 09:03 AM
Here is the code snippet to do your job.
Assume m_EditArea is your CRichEditCtrl Variable


LOGFONT lf;
CFont* pFont;
CHARFORMAT cf, newcf;
DWORD mask;
long start, end;
CDC *ClientDC;

ClientDC = this->GetDC();
memset(&cf, 0, sizeof(CHARFORMAT));
memset(&newcf, 0, sizeof(CHARFORMAT));
cf.cbSize = sizeof(CHARFORMAT);
newcf.cbSize = sizeof(CHARFORMAT);
memset(&lf, 0, sizeof(LOGFONT));
pFont = m_EditArea.GetFont();
mask = m_EditArea.GetSelectionCharFormat(cf);
if (!mask)
mask = cf.dwMask;
if (!pFont)
pFont = GetFont();
pFont->GetLogFont(&lf);
strcpy(lf.lfFaceName, cf.szFaceName);
if (cf.dwEffects & CFE_BOLD)
lf.lfWeight = FW_BOLD;
else
lf.lfWeight = FW_NORMAL;
if (cf.dwEffects & CFE_ITALIC)
lf.lfItalic = true;
if (cf.dwEffects & CFE_UNDERLINE)
lf.lfUnderline = true;
if (cf.dwEffects & CFE_STRIKEOUT)
lf.lfStrikeOut = true;
// convert twips into point size
int PtHeight = (cf.yHeight/20);
// convert pointsize into logical units
lf.lfHeight = -MulDiv (PtHeight, GetDeviceCaps (ClientDC->m_hDC, LOGPIXELSY), 72);
CFontDialog dlg(&lf);
dlg.m_cf.Flags |= CF_EFFECTS;
dlg.m_cf.rgbColors = cf.crTextColor;
if (dlg.DoModal() == IDOK)
{
strcpy(newcf.szFaceName, dlg.GetFaceName());
newcf.dwMask |= CFM_FACE;
newcf.yHeight = (dlg.GetSize()/10) * 20; // convert into twips
newcf.dwMask |= CFM_SIZE;
newcf.crTextColor = dlg.GetColor();
newcf.dwMask |= CFM_COLOR;
newcf.dwMask |= CFM_BOLD;
if (dlg.IsBold()) {
newcf.dwEffects |= CFE_BOLD;
}
newcf.dwMask |= CFM_ITALIC;
if (dlg.IsItalic()) {
newcf.dwEffects |= CFE_ITALIC;
}
newcf.dwMask |= CFM_UNDERLINE;
if (dlg.IsUnderline()) {
newcf.dwEffects |= CFE_UNDERLINE;
}
newcf.dwMask |= CFM_STRIKEOUT;
if (dlg.IsStrikeOut()) {
newcf.dwEffects |= CFE_STRIKEOUT;
}
m_ruleArea.SetSelectionCharFormat(newcf);
}





Hope that helps
mfcUser