Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 23-Apr-17 22:24pm

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);
}
 
Share this answer
 
Your test looks wrong; surely it should return TRUE to indicate an alphabetic character. Also, why use hex rather than actual characters? So ...
C++
if ((pMsg->wParam >= 'A' && pMsg->wParam <= 'Z') ||
    (pMsg->wParam >= 'a' && pMsg->wParam <= 'z'))
{
    return TRUE;
}

You can see what actual value is in wParam by using your debugger.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900