MFC窗口键盘和鼠标消息拦截

void C键盘消息Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值

    if((nFlags | MK_CONTROL) ==  nFlags ){
        AfxMessageBox(_T("我同时按下了contrl键"));
    }

    if((nFlags | MK_CONTROL | MK_SHIFT) ==  nFlags ){//进行或运算  010 | 010  = 010
        AfxMessageBox(_T("我同时按下了contrl+shift键"));
    }

    CString s;
    s.Format(_T("我是左键点击鼠标,我的x坐标是%d,我的y坐标是%d\r\n"),point.x,point.y);//这个坐标是相对于窗口的坐标,不算标题工具栏哦
    AfxMessageBox(s);


    CDialog::OnLButtonDown(nFlags, point);
}

BOOL C键盘消息Dlg::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message == WM_KEYDOWN ){//键盘按下的事件
        AfxMessageBox(_T("我按下了键盘的键"));
        switch(pMsg->wParam){
            case VK_NUMPAD9://这里面的消息都是大写,字母全是大写哦,如果要判断字母的 大小写就得到WM_CHAR消息里面去看
                AfxMessageBox(_T("我按下了键盘的小键盘9"));        //msdn搜索:Virtual
                break;
        }
    }else if(pMsg->message == WM_CHAR){//这个WM_CHAR消息只能接受a-z_0-9,可以输出到界面上的字符
        if(IsCharUpper(pMsg->wParam) == TRUE){//IsCharUpper如果是大写则返回TRUE
            
            AfxMessageBox(_T("我是大写"));
        }else if(IsCharLower(pMsg->wParam) == TRUE){
        AfxMessageBox(_T("我是小写"));
        }
        CString a;
        a.Format(_T("我是数字%d"),pMsg->wParam);
        AfxMessageBox(a);
        char c = (char)pMsg->wParam;//把数字强制转换成char类型,也就是字母

        CString a1;
        a1.Format(_T("我是字母%c"),c);
        AfxMessageBox(a1);
    }
    return CDialog::PreTranslateMessage(pMsg);
}

 

 

重写 按键和鼠标消息 进行拦截

先来讲鼠标
我R,卡了

MK_CONTROL Set if the CTRL key is down.

MK_LBUTTON Set if the left mouse button is down.

MK_MBUTTON Set if the middle mouse button is down.

MK_RBUTTON Set if the right mouse button is down.

MK_SHIFT Set if the SHIFT key is down.


MFC的对话框的 重写按键的消息无效,只能重写PreTrancemaner函数

接下来讲键盘按下事件,MFC的对话框的 重写按键的消息无效

posted @ 2013-08-05 21:10  宝贝,我永远都在  阅读(2063)  评论(0)    收藏  举报