【问题标题】:Handling arrow key in cedit control of mfc在 mfc 的 cedit 控制中处理箭头键
【发布时间】:2013-12-26 06:32:36
【问题描述】:

我正在使用 mfc 的 cedit 控件制作类似 matlab 的命令窗口。

例如,在我输入几个命令后,我想使用箭头键(特别是向上键)显示旧命令。

我成功显示旧命令,但未能将光标定位在该命令的末尾。 原因似乎是我将光标定位在该命令的末尾后又输入了一次箭头键。

这里是详细情况。

  • 首先我输入命令“播放”
  • 然后播放!...消息弹出。
  • 然后在下一个命令提示符中,我点击了“↑”键
  • 我成功地使用了自动流式传输的旧命令“播放”,

但是,我的光标向上移动了。

@播放

播放!.. | (←光标位于此处..)

@播放| (←我想把光标定位到这里,点击'↑'键后)

这是我的代码:

class CEditCommand::CEdit
{
public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
}

BOOL CEditCommand::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        if (pMsg->wParam == VK_UP)
        {
            int nS = 0; nE =0;
            GetSel(nS, nE);
            int nLineIndex = LineIndex();
            CString str = m_CommandHistory[m_nCommandIndex];
            m_nCommandIndex--;
            SetSel(nLineIndex, nE);
            ReplaceSel(str);
            SetSel(0, -1);
            SetSel(-1, -1);
        }
    }
}

我不知道为什么在执行PreTranslateMessage后会再次按下'↑'键。 有人对此有想法吗?

【问题讨论】:

    标签: mfc keyboard cedit


    【解决方案1】:

    您的编辑控件仍会收到向上箭头消息,因此当pMsg->wParam == VK_UP 时,您需要在CEditCommand::PreTranslateMessage() 中为WM_KEYDOWNWM_KEYUP 返回TRUE

    【讨论】:

    • 感谢您的帮助。解决方案真的很简单。
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多