Search code examples
c++mfcwindows-messagescricheditctrlcrichedit

Change char inserted using Alt+Unicode in CRichEdit


I want to change a unicode char inserted using Alt+Unicode code from the keyboard. I used PretranslateMessage for changing the chars inserted directly from the keyboard and it worked. But with the Alt+Unicode code method it does not. Here is the code: Microsoft Word has this functionality when enabling show/hide paragraph marks.

BOOL CEmphasizeEdit::PreTranslateMessage(MSG* msg)
{
    if (msg->hwnd == m_hWnd)
    {
        if (msg->message == WM_CHAR)
        {
            if (TheApp.Options.m_bShowWSpaceChars)
            {
                if (msg->wParam == ' ')  // This works in both cases Space key pressed or Alt + 3 + 2 in inserted
                {
                    msg->wParam = '·';
                }
                else if (msg->wParam == (unsigned char)' ') // this does not work
                {
                    msg->wParam = (unsigned char)'°'; 
                }
            }
        }
    }
    return CRichEditCtrl::PreTranslateMessage(msg);
}

If I insert from the keyboard Alt + 0 + 1 + 6 + 0 which is ' '(No-Break Space), I want the CRichEditCtrl to display '°' or another char that I specify.

How can I handle this to make it work?


Solution

  • I had to get the cursor position send an append string to the control and then set the selection after the inserted character. When this happens I have to skip CRichEditCtrl::PreTranslateMessage(msg);

    BOOL CEmphasizeEdit::PreTranslateMessage(MSG* msg)
    {
        if (msg->hwnd == m_hWnd)
        {
            if (msg->message == WM_CHAR)
            {
                TCHAR text[2];
                text[1] = 0x00;
                BOOL found = 1;
    
                switch (msg->wParam)
                {
                    case 0x20: text[0] = _C('·'); break;
                    case 0xA0: text[0] = 0xB0; break;
                }
    
                CHARRANGE cr;
                GetSel(cr);
                cr.cpMax++;
                cr.cpMin++;
    
                ReplaceSel(text);
                SetSel(cr);
    
                return 1;
            }
        }
        return CRichEditCtrl::PreTranslateMessage(msg);
    }