找了几篇相关文章,方法是重写虚函数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 default
CString 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);
}