Change char inserted using Alt+Unicode in CRichEdit

心已入冬 提交于 2020-02-06 03:30:31

问题


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?


回答1:


Alt+Space is reserved for program's close menu.

You should use another sequence like Ctrl+Space or Alt+Ctrl+Space

' ' and (unsigned char)' ' are the same thing, therefore the code never reaches else if (msg->wParam == (unsigned char)' '). You should remove that.

Use GetAsyncKeyState to see if Alt or Ctrl key is pressed down.

BOOL IsKeyDown(int vkCode)
{
    return GetAsyncKeyState(vkCode) & 0x8000;
}

...
if (msg->wParam == ' ')
{
    if (IsKeyDown(VK_CONTROL))
        msg->wParam = L'°'; 
    else
        msg->wParam = L'+';
}
...



回答2:


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);
}


来源:https://stackoverflow.com/questions/32523215/change-char-inserted-using-altunicode-in-crichedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!