//得到控件类型
CtrlType CENTERView::GetCtrlType(CWnd * pWnd)
{
char buf[512];
CString szTmp;
//定义枚举类型
enum CtrlType
{
EditType, //编辑框
ButtonType, //按钮
OtherType //其它
};
给这个基类添加一个保护方法:
if( pWnd != NULL )
{
::GetClassName( pWnd->GetSafeHwnd(), buf, 512);
szTmp = buf;
szTmp.TrimRight();
if( szTmp == "Edit" )
return EditType;
else if( szTmp == "Button" )
return ButtonType;
else
return OtherType;
}
else
return OtherType;
}
BOOL CENTERView::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message == WM_KEYDOWN)
{
CWnd * pCtrl = NULL;
switch(pMsg->wParam)
{
case VK_ESCAPE://屏蔽ESC
return TRUE;
case VK_RETURN:
{
pCtrl = GetFocus();
//如果当前控件是按钮 就模点击
if( GetCtrlType(pCtrl) == ButtonType )
{
::SendMessage( pCtrl->GetSafeHwnd(), WM_LBUTTONDOWN, 0, 0);
::SendMessage( pCtrl->GetSafeHwnd(), WM_LBUTTONUP, 0, 0);
}
if( GetCtrlType(pCtrl) != OtherType )
GetNextDlgTabItem(pCtrl)->SetFocus();
return TRUE;
}
default:
break;
}
}
return CFormView::PreTranslateMessage(pMsg);
}