只接受a到z和a到Z(只有alpabatic字符) [英] Accepting only a to z and a to Z (only alpabatic characters)
问题描述
如何实现只访问vc ++中的字母字符mfc
我尝试过:
BOOL CFilterGroup :: PreTranslateMessage(MSG * pMsg)
{
if(pMsg-> message == WM_CHAR)
{
if((pMsg-> wParam> ='\ x41'&& pMsg-> wParam< ='\ x5A')) />
{
返回FALSE;
}
}
返回CDialogEx :: PreTranslateMessage(pMsg);
}
但它不会忽略并且它取所有字符
你必须反转检查和返回值。
参见 CWnd :: PreTranslateMessage [ ^ ]:
引用:如果邮件已翻译且不应发送,则为非零; 0如果邮件未被翻译并且应该被分派。
如果要过滤掉邮件,你必须返回
TRUE
。
要检查小写和大写ASCII字母(az,AZ),可以使用 isalpha [ ^ ]
#include< ctype.h>
BOOL CFilterGroup :: PreTranslateMessage(MSG * pMsg)
{
if(pMsg-> message == WM_CHAR)
{
if(! isalpha(pMsg-> wParam))
{
/ *返回TRUE表示不应该分派该消息。 * /
返回TRUE;
}
}
返回CDialogEx :: PreTranslateMessage(pMsg);
}
你的测试看起来不对;当然它应该返回TRUE
来表示字母字符。另外,为什么使用十六进制而不是实际字符?所以...
if ((pMsg-> wParam> = ' A'&& pMsg-> wParam< = ' Z')||
(pMsg-> wParam> = ' a'&& pMsg-> wParam< = ' z'))
{
return TRUE;
}
您可以使用调试器查看wParam
中的实际值。
How to implements to access only alphabetic characters in vc++ mfc
What I have tried:
BOOL CFilterGroup::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_CHAR)
{
if ((pMsg->wParam >= '\x41' && pMsg->wParam <= '\x5A'))
{
return FALSE;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
but its not ignoring and its taking all characters
You have to reverse the check and the return value.
See CWnd::PreTranslateMessage[^]:
Quote:Nonzero if the message was translated and should not be dispatched; 0 if the message was not translated and should be dispatched.
You have to return
TRUE
if a message should be filtered out.
To check for lower and upper case ASCII letters (a-z, A-Z), you can use isalpha[^]
#include <ctype.h> BOOL CFilterGroup::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_CHAR) { if (!isalpha(pMsg->wParam)) { /* Return TRUE to indicate that message should not be dispatched. */ return TRUE; } } return CDialogEx::PreTranslateMessage(pMsg); }
Your test looks wrong; surely it should returnTRUE
to indicate an alphabetic character. Also, why use hex rather than actual characters? So ...
if ((pMsg->wParam >= 'A' && pMsg->wParam <= 'Z') || (pMsg->wParam >= 'a' && pMsg->wParam <= 'z')) { return TRUE; }
You can see what actual value is inwParam
by using your debugger.
这篇关于只接受a到z和a到Z(只有alpabatic字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!