2

In an MFC dialog I have a RichEditCtrl (2.0). Let's asume I want to highlight each 1 within the text field. Therefore, in the dialog class, I listen to the ctrl's EN_CHANGE message and call the following function

void CParentDlg::OnEnChange()
{
  CHARRANGE sel;
  mRichEdit.GetSel(sel);

  CHARFORMAT2 numCharFormat;
  mRichEdit.GetDefaultCharFormat(numCharFormat);
  numCharFormat.dwMask |= CFM_COLOR;
  numCharFormat.dwEffects &= ~CFE_AUTOCOLOR;
  numCharFormat.crTextColor = RGB(0, 255, 0);

  CHARFORMAT2 defaultCharFormat;
  mRichEdit.GetDefaultCharFormat(defaultCharFormat);


  CString text;
  mRichEdit.GetWindowText(text);
  for (int i = 0; i < text.GetLength(); ++i)
  {
    wchar_t c = text[i];
    CHARRANGE cR = { i, i + 1 };
    mRichEdit.SetSel(cR);
    if (c == L'1')
    {
      mRichEdit.SetWordCharFormat(numCharFormat);
    }
    else
    {
      mRichEdit.SetWordCharFormat(defaultCharFormat);
    }
  }

  mRichEdit.SetSel(sel);
}

Here, mRichEdit is a CRichEditCtrl member of CParentDlg. Coloring works fine.

The problem is, that all the intermediate selections and colorings are pushed on the undo stack. When pressing Ctrl+Z, not the last letter is being removed but the last selection or color reverted.

Is it possible to ignore the selections and colorings for the undo stack? Or do I have to implement an undo stack myself?

The textmode of the control is 42 (sic!), i.e. TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE

chrizke
  • 458
  • 3
  • 10

0 Answers0