ExampleTo specify that you want the member function OnKeydownList1 to handle LVN_KEYDOWN messages from the CListCtrl whose ID is IDC_LIST1, you would use ClassWizard to add the following to your message map:ON_NOTIFY( LVN_KEYDOWN, IDC_LIST1, OnKeydownList1 )In the example above, the function provided by ClassWizard is:void CMessageReflectionDlg::OnKeydownList1(NMHDR* pNMHDR, LRESULT* pResult) { LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR; // TODO: Add your control notification handler // code here
{
LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
// TODO: Add your control notification handler
// code here
*pResult = 0;
}
我的要求是:通过键盘上下键改变列表选项,然后按回车键弹出一个对话框,上面已经说过VK_UP,VK_DOWN可以响应,VK_RETURN不能响应,可是如果你用鼠标点下不要抬起,再按回车键VK_RETURN就能响应了,不知何故,这显然不是我要求的
{
if (pMsg->message==WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_DOWN:
pMsg->wParam=0; // 使键值为0,防止其他控件再处理
m_List.SetCurSel(m_List.GetCurSel() + 1); break;
case VK_UP:
pMsg->wParam=0;
if(m_List.GetCurSel() != 0)
m_List.SetCurSel(m_List.GetCurSel() - 1); break;
case VK_ESCAPE:
SendMessage(WM_CLOSE);
AfxGetMainWnd()->SetFocus(); break;
case VK_RETURN:
pMsg->wParam=0;
CString str;
int nSel = m_List.GetCurSel(); // 防止选中时无数据
if(nSel >= 0)
{
m_List.GetText(nSel,str);
SendMessage(WM_CLOSE);
if(!str.IsEmpty())
AfxMessageBox(str);
} break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}