简洁的方法可以用PreTranslateMessage()捕捉WM_CHAR消息。
再用::GetDlgCtrlID(pMsg->hwnd);取Edit控件的ID号
其中:
pMsg->message为捕捉到的消息。
键入字符在pMsg->wParam里。
BOOL CAddressAssistDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
int id;
char ch;
switch (pMsg->message)
{
case WM_CHAR:
id = ::GetDlgCtrlID(pMsg->hwnd);//取控件ID号
ch = pMsg->wParam;//从消息中取出字符
::CharUpper(&ch);//转换为大写字母
pMsg->wParam = ch;//改写字符消息
switch (id)
{
case IDC_EDIT1:
case IDC_EDIT2:
case IDC_EDIT4:
case IDC_EDIT5:
case IDC_EDIT7:
case IDC_EDIT8:
if (!((ch >= '0' && ch <= '9') ||
(ch >= 'A' && ch <= 'F') ||
(ch == VK_DELETE) ||
(ch == VK_BACK)
)
)
{
MessageBox("请输入数字0~9,A~F");
return TRUE;//出错,放弃键入的非法字符
}
break;
case IDC_EDIT3:
case IDC_EDIT6:
case IDC_RECV_AREA:
MessageBox("请不要输入!!!");
return TRUE;//出错,放弃键入的非法字符
case IDC_SEND_AREA:
if (!((ch >= '0' && ch <= '9') ||
(ch >= 'A' && ch <= 'F') ||
(ch == ' ') ||
(ch == 'W') ||
(ch == 'I') ||
(ch == 'O') ||
(ch == 'T') ||
(ch == VK_RETURN) ||
(ch == VK_DELETE) ||
(ch == VK_BACK)
)
)
{
MessageBox("请输入合法字符及命令0~9,A~F, ' ',I,O,W,T");
return TRUE;//出错,放弃键入的非法字符
}
break;
}
break;
}
return CDialog::PreTranslateMessage(pMsg);
}
文章评论(0条评论)
登录后参与讨论