(一),新建一个单文档工程CtrlEg。
(二),为CCtrlEgView的WM_KEYDOWN消息和WM_KEYUP消息添加响应函数。具体代码如下:
void CCtrlEgView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { { CString strMess ; strMess.Format("CCtrlEgView::OnKeyDown nChar = %d nFlags = %x/n",nChar,nFlags); TRACE(strMess); } CView::OnKeyDown(nChar, nRepCnt, nFlags); } void CCtrlEgView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { { CString strMess ; strMess.Format("CCtrlEgView::OnKeyUp nChar = %d nFlags = %x/n",nChar,nFlags); TRACE(strMess); } CView::OnKeyUp(nChar, nRepCnt, nFlags); }
(三)只按Ctrl键,结果如下:
CCtrlEgView::OnKeyDown nChar = 17 nFlags = 1d CCtrlEgView::OnKeyUp nChar = 17 nFlags = c01d
(四)按Ctrl+v,结果如下:
CCtrlEgView::OnKeyDown nChar = 17 nFlags = 1d CCtrlEgView::OnKeyUp nChar = 17 nFlags = c01d CCtrlEgView::OnKeyUp nChar = 86 nFlags = c02f
(五),删除“粘贴”命令的快捷键,具体操作是从快捷键资源IDR_MAINFRAME中删除项ID_EDIT_PASTE。再按Ctrl+v键,结果如下:
CCtrlEgView::OnKeyDown nChar = 17 nFlags = 1d CCtrlEgView::OnKeyDown nChar = 86 nFlags = 2f CCtrlEgView::OnKeyUp nChar = 17 nFlags = c01d CCtrlEgView::OnKeyUp nChar = 86 nFlags = c02f
附:
#define VK_CONTROL 0x11 VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)