VC++6.0 CEdit 响应键盘输入事件
摘自:http://blog.csdn.net/howard_liu1314/article/details/8222357
项目中要对对话框的键盘按键做出相应,实现逻辑如下:
VS2005开发MFC的wince项目;在对话框中‘重写’ PreTranslateMessage方法;
1.头文件中添加:virtual BOOL PreTranslateMessage(MSG* pMsg);
2.cpp文件的实现代码:
BOOL CSystemSetupDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN)
{
UINT iKey=(UINT)pMsg->wParam;
switch(iKey)
{
case VK_F1:
//可以用GetKeyState()检测按键状态
if(GetKeyState(VK_CONTROL)<0)
{
//CTRL+F1的处理逻辑
}
break;
case VK_F2:
if(GetKeyState(VK_CONTROL)<0)
{
//CTRL+F2的处理逻辑
}
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
这样对话框就能相应键盘按键消息了。
3.CEDIT控件只允许输入数字
BOOL CXXDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO:Add your specialized code here and/or call the base class
CEdit*pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
ASSERT(pEdit &&pEdit->GetSafeHwnd());
//可以使用pEdit->m_hwnd替代GetSafeHwnd
if((WM_KEYDOWN == pMsg->message)&&(pEdit->GetSafeHwnd() ==pMsg->hwnd))
{
if((pMsg->wParam >= _T('0')&& pMsg->wParam<= _T('9'))
|| ((pMsg->wParam >= VK_NUMPAD0)&& (pMsg->wParam<= VK_NUMPAD9))
|| (_T('*') == pMsg->wParam) || (_T('#') ==pMsg->wParam)
|| (VK_LEFT == pMsg->wParam) || (VK_RIGHT ==pMsg->wParam)
|| (VK_UP == pMsg->wParam) || (VK_DOWN ==pMsg->wParam)
|| (VK_DELETE == pMsg->wParam) || (VK_BACK ==pMsg->wParam))
{
return CDialog::PreTranslateMessage(pMsg);
}
else
{
return TRUE;
}
}
returnCDialog::PreTranslateMessage(pMsg);
}
4.控件知识
#define __MY_VK_H__
//却没有对它们进行定义,所以我们只好自己来定义了
//定义其实挺简单的,定义的数值为相应的字符的ASCII码的16进制数据
//* VK_0 - VK_9 are the same as ASCII '0' - '9' (0x30 - 0x39)
//* 0x40 : unassigned
//* VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)
#define VK_0 0x30
#define VK_1 0x31
#define VK_2 0x32
#define VK_3 0x33
#define VK_4 0x34
#define VK_5 0x35
#define VK_6 0x36
#define VK_7 0x37
#define VK_8 0x38
#define VK_9 0x39
#define VK_A 0x41
#define VK_B 0x42
#define VK_C 0x43
#define VK_D 0x44
#define VK_E 0x45
#define VK_F 0x46
#define VK_G 0x47
#define VK_H 0x48
#define VK_I 0x49
#define VK_J 0x4A
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_M 0x4D
#define VK_N 0x4E
#define VK_O 0x4F
#define VK_P 0x50
#define VK_Q 0x51
#define VK_R 0x52
#define VK_S 0x53
#define VK_T 0x54
#define VK_U 0x55
#define VK_V 0x56
#define VK_W 0x57
#define VK_X 0x58
#define VK_Y 0x59
#define VK_Z 0x5A
#define VK_a 0x61
#define VK_b 0x62
#define VK_c 0x63
#define VK_d 0x64
#define VK_e 0x65
#define VK_f 0x66
#define VK_g 0x67
#define VK_h 0x68
#define VK_i 0x69
#define VK_j 0x6A
#define VK_k 0x6B
#define VK_l 0x6C
#define VK_m 0x6D
#define VK_n 0x6E
#define VK_o 0x6F
#define VK_p 0x70
#define VK_q 0x71
#define VK_r 0x72
#define VK_s 0x73
#define VK_t 0x74
#define VK_u 0x75
#define VK_v 0x76
#define VK_w 0x77
#define VK_x 0x78
#define VK_y 0x79
#define VK_z 0x7A