c++ - 在 CRichEdit 中使用 Alt+Unicode 更改插入的字符

标签 c++ mfc windows-messages cricheditctrl crichedit

我想更改使用键盘上的 Alt+Unicode 代码插入的 unicode 字符。 我使用 PretranslateMessage 来更改直接从键盘插入的字符并且它起作用了。但使用 Alt+Unicode 代码方法则不然。 这是代码: Microsoft Word 在启用显示/隐藏段落标记时具有此功能。

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

如果我从键盘 Alt + 0 + 1 + 6 + 0 插入 ' '(No-Break Space),我希望 CRichEditCtrl 显示 '°' 或我指定的其他字符。

我该如何处理才能使其发挥作用?

最佳答案

Alt+Space 为程序的关闭菜单预留。

您应该使用另一个序列,例如 Ctrl+SpaceAlt+Ctrl+Space

' '(unsigned char)' ' 是同一件事,因此代码永远不会到达 else if (msg->wParam == (unsigned字符)'')。你应该删除它。

使用 GetAsyncKeyState 查看是否按下了 AltCtrl 键。

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

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

关于c++ - 在 CRichEdit 中使用 Alt+Unicode 更改插入的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523215/

相关文章:

windows - 如何在表格中心模仿[Ctrl+鼠标左键]或打开另一个程序并输入一个词?

winapi - 为什么GetMessage不处理WM_POWERBROADCAST消息?

c++ - SIMD 的偏好是始终摆脱分支吗?

c++ - 无法从大括号括起来的初始值设定项列表转换为 std::vector

c++ - 应如何定义带有 const ** 参数的函数,以尽量减少调用者对不必要的 const_casts 的需求?

c++ - 将位图设置为 CStatic 对象

c++ - MFC:环 - 无颜色变化

c++ - 为什么在使用 vfork() 时会出现错误?

c++ - 如何调整 CListCtrl 列的宽度以适应每列中最长的字符串?

windows - 鼠标单击是 WM_* 消息还是向上和向下消息的组合?