Valhalla Legends Forums Archive | C/C++ Programming | HFONT and CHARFORMAT

AuthorMessageTime
Okee
Hey guys, I've got all my font attributes stored in an LOGFONT struct. This works perfect for the SETFONT message being sent to my LivtView, and EDIT controls but not so much for my rich edit control.

The AppendText function I am using uses a CHARFORMAT struct to set the color and font of the text is prints. I kind of want get away from using the CHARFORMAT struct, so I can use this LOGFONT struct for the rich control text as well. My question is - how would I print text a rich edit control and format it with an LOGFONT? I use CreateFontIndirect to get my HFONT out of the LOGFONT struct, then send the SETFONT message.

Is there any way to print text to a rich edit control using either a LOGFONT or HFONT, instead of CHARFORMAT? Or, is there a way to get the values needed for CHARFORMAT out of the LOGFONT?

Thanks in advance guys.
July 30, 2005, 8:47 PM
Okee
Actually, it's much simpler than I thought. You can simply use the LOGFONT values in place of the CHARFORMAT values. One problem I'm having though is that when I change the font and font size, save the values in the LOGFONT, then put the LOGFONT values into the CHARFORMAT struct.. the font being displayed will be in the new font face, but the size will be the same. Here's the code the CharFormat function..

[code]
void CharFormat(COLORREF Color, CHARFORMAT &cfFormat)
{
cfFormat.cbSize = sizeof(CHARFORMAT);
cfFormat.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_CHARSET;
cfFormat.dwEffects = CFE_PROTECTED;
cfFormat.yHeight = lfChatFont.lfHeight;
cfFormat.yOffset = 0;
cfFormat.crTextColor = Color;
cfFormat.bCharSet = lfChatFont.lfCharSet;
cfFormat.bPitchAndFamily = lfChatFont.lfPitchAndFamily;
strcpy(cfFormat.szFaceName, lfChatFont.lfFaceName);
}
[/code]

and the appendtext function...

[code]
void __cdecl AppendText(HWND hRichEdit, COLORREF Color, char *szFmt, ...)
{
char szTxtToAppend[2048] = "";
va_list vaArg;
va_start(vaArg, szFmt);
vsprintf(szTxtToAppend, szFmt, vaArg);
va_end(vaArg);

CHARFORMAT cfFormat;
CHARRANGE CharRange = {-1, -1};
SendMessage(hRichEdit, EM_EXSETSEL, 0, (LPARAM)&CharRange);
CharFormat(Color, cfFormat);
SendMessage(hRichEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfFormat);
SendMessage(hRichEdit, EM_REPLACESEL, FALSE, (LPARAM)szTxtToAppend);
SendMessage(hRichEdit, EM_SCROLL, SB_LINEDOWN, 0);
}
[/code]

The lfChatFont is my global LOGFONT struct. I print the values of lfChatFont.lfFontHeight and they do change when I change the size, but the size displayed stays the same? Anyone know why?
July 31, 2005, 12:07 AM

Search