CEditView处理回车键

来源:互联网 发布:天缘微信恢复软件 编辑:程序博客网 时间:2024/05/21 12:34

找了几篇相关文章,方法是重写虚函数PreTranslateMessage,


BOOL CMyEditView::PreTranslateMessage(MSG* pMsg)
{
    // TODO: Add your specialized code here and/or call the base class
    if (WM_KEYFIRST <= pMsg-> message && pMsg-> message <= WM_KEYLAST)
    {
        if(pMsg->wParam==VK_RETURN )
        {
            UpdateData(TRUE);
            AfxMessageBox("Hello World");
        }
    }

    
    return CEditView::PreTranslateMessage(pMsg);
}

测试的时候发现、使用TranslateMessage,消息会来2次。怎么办都没法消除。所以改换用WM_KEYDOWN消息。

void CMyEditView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {// TODO: Add your message handler code here and/or call defaultCString theStr;if(nChar==VK_RETURN ) {CString windowText;CMyEdit& theEdit = (CMyEdit&)GetEditCtrl();theEdit.GetWindowText(windowText);CString str = windowText;int n = str.ReverseFind('>');str = str.Right(str.GetLength()-n-1);CMyPacket thePacket(str);thePacket.SendData(m_socket);}CEditView::OnKeyDown(nChar, nRepCnt, nFlags);}


原创粉丝点击