用ENTER键切换控件焦点,并模拟按键

类别:VC语言 点击:0 评论:0 推荐:
//得到控件类型
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);
}

本文地址:http://com.8s8s.com/it/it424.htm