MFC编辑框中按下回车后使光标换行

来源:互联网 发布:存货毕业设计数据分析 编辑:程序博客网 时间:2024/05/17 03:25
MFC编辑框中输入字符,按下回车,发现光标并没有按照想象的另起一行,需要手动截获回车按键并添加对应的换行处理。Dialog对话框中截获消息可以覆盖父类的PreTranslateMessage方法,故实现方法如下例所示(编辑软件是VS2010):
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg){    // TODO: Add your specialized code here and/or call the base class    // 判断是否按下键盘Enter键    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)    {        // 获取当前的焦点是否是Edit编辑框        if (GetFocus() == GetDlgItem(IDC_EDIT_DEBUGAT))        {            /*              * m_edit_debugat和m_str_debugat分别是             * IDC_EDIT_DEBUGAT所对应控件相关的类变量:             * CEdit    m_edit_debugat;             * CString  m_str_debugat;            */            m_edit_debugat.GetWindowTextA(m_str_debugat);            // 给显示字符串添加回车和换行            m_str_debugat += "\r\n";            m_edit_debugat.SetWindowTextA(m_str_debugat);            int len = m_str_debugat.GetLength();            // 设置光标位置            // 第一个参数是显示字符串选中部分的起始位置            // 第二个参数是显示字符串选中部分的结束位置            // 两个参数相等,代表不选中任何字符,光标指向对应字符处            m_edit_debugat.SetSel(len, len);        }        return TRUE;    }    return CDialog::PreTranslateMessage(pMsg);}
0 0