SetSelectionCharFormat difficulties when selection contains different charset
-
I am using
SetSelectionCharFormat
in aRichEditCtrl
object on my dialog box to allow a user to change the font/bold/etc text he is typing in the rich edit box. Basically the function should change the character formatting as needed; the code is as follows:void CRichEditProjDlg::OnSelchangeFontsize()
{
DWORD Selection=0, FontSize=0;
CString SelFontSize;
Selection = m_FontSize.GetCurSel(); //Get the current Selectionm\_FontSize.GetLBText(Selection, SelFontSize); //Get the requested Font Size from a drop down box FontSize = \_ttoi(SelFontSize); CHARFORMAT Cfm; Cfm.cbSize = sizeof(CHARFORMAT); m\_RichCatch.GetSelectionCharFormat(Cfm); //Get the char set of the selection Cfm.yHeight = FontSize\*20; //Calculate the new font size m\_RichCatch.SetSelectionCharFormat(Cfm); //Set the new font size m\_RichCatch.SetFocus(); //Update the Rich Edit Box UpdateData(TRUE);
}
void CRichEditProjDlg::OnBold()
{
CHARFORMAT Cfm;
Cfm.cbSize = sizeof(CHARFORMAT);m\_RichCatch.GetSelectionCharFormat(Cfm); //Get the char set of selection Cfm.dwMask = CFM\_BOLD; //Apply "boldness" Cfm.dwEffects ^= CFE\_BOLD; m\_RichCatch.SetSelectionCharFormat(Cfm); //Set the char set m\_RichCatch.SetFocus();
}
This bold-ing function works fine, but the Font size one only works when the selected text are all of the same font size. For example, if all of the selected text are of font size 10, I selected all of them and chose Font size 20, they would all change to font size 20. But if the selected text was of differing font sizes, say the first few words were font size 10 and the rest font size 11, the code does not change the font size of any of the selelcted text. Am I approaching the function in the wrong way? Or must I do an internal check for different font sizes and change them all separately somehow? Please feel free to ask for more information if needed. Thank you.
-
I am using
SetSelectionCharFormat
in aRichEditCtrl
object on my dialog box to allow a user to change the font/bold/etc text he is typing in the rich edit box. Basically the function should change the character formatting as needed; the code is as follows:void CRichEditProjDlg::OnSelchangeFontsize()
{
DWORD Selection=0, FontSize=0;
CString SelFontSize;
Selection = m_FontSize.GetCurSel(); //Get the current Selectionm\_FontSize.GetLBText(Selection, SelFontSize); //Get the requested Font Size from a drop down box FontSize = \_ttoi(SelFontSize); CHARFORMAT Cfm; Cfm.cbSize = sizeof(CHARFORMAT); m\_RichCatch.GetSelectionCharFormat(Cfm); //Get the char set of the selection Cfm.yHeight = FontSize\*20; //Calculate the new font size m\_RichCatch.SetSelectionCharFormat(Cfm); //Set the new font size m\_RichCatch.SetFocus(); //Update the Rich Edit Box UpdateData(TRUE);
}
void CRichEditProjDlg::OnBold()
{
CHARFORMAT Cfm;
Cfm.cbSize = sizeof(CHARFORMAT);m\_RichCatch.GetSelectionCharFormat(Cfm); //Get the char set of selection Cfm.dwMask = CFM\_BOLD; //Apply "boldness" Cfm.dwEffects ^= CFE\_BOLD; m\_RichCatch.SetSelectionCharFormat(Cfm); //Set the char set m\_RichCatch.SetFocus();
}
This bold-ing function works fine, but the Font size one only works when the selected text are all of the same font size. For example, if all of the selected text are of font size 10, I selected all of them and chose Font size 20, they would all change to font size 20. But if the selected text was of differing font sizes, say the first few words were font size 10 and the rest font size 11, the code does not change the font size of any of the selelcted text. Am I approaching the function in the wrong way? Or must I do an internal check for different font sizes and change them all separately somehow? Please feel free to ask for more information if needed. Thank you.
Ummm - maybe you should set
Cfm.dwMask
toCFM_SIZE
when setting the font size? Also - why bother getting the selection's char format, when you can specify only the attributes you want to set within theCHARFORMAT
structure? -
Ummm - maybe you should set
Cfm.dwMask
toCFM_SIZE
when setting the font size? Also - why bother getting the selection's char format, when you can specify only the attributes you want to set within theCHARFORMAT
structure?