Search code examples
c++colorsmfccricheditctrl

How could I change the highlight color in a CRichEditCtrl?


I've tried to catch the EN_SelChange event, but it never triggers although I have added

rich->SetEventMask(rich->GetEventMask() | ENM_CHANGE | ENM_SELCHANGE);

in OnInitDialog fxn. So I tried to add the following code in my OnEraseBkgnd function.

BEdit *edit = (BEdit *)GetDlgItem(IDC_MAIN_EDIT1);
CRichEditCtrl *ctrl = &(edit->GetRichEditCtrl());
long s = 0 , e = 0;
ctrl->GetSel(s, e);

and the program crashed on GetSel function

I'm now figuring out one way to change the text highlight color when it get selected.


Solution

  • I have figured one way to solve this problem. Catch the selection message from message loop:

    BOOL CNotepadDlg::PreTranslateMessage(MSG*   pMsg)
    {
     if (pMsg->message == 514||(pMsg->message == 257 && (pMsg->wParam == 40|| pMsg->wParam == 39 || pMsg->wParam == 38 || pMsg->wParam == 37)))
    {
        CString str;
        BEdit *edit = (BEdit *)GetDlgItem(IDC_MAIN_EDIT1);
        edit->GetRichEditCtrl().GetSel(start, end);
        ......
    }
    ......
    }
    

    514 is the mouse selection event, 257 is the code for keyboard event. When the message is 257 the wParam stands for the key that provokes that event. By this way we can catch the selection event although it's not elegant enough...

                                                     Bill Sun