我在MFC中,加入了一个edit框,把它的NUMBER属性设置为true,限制只能输入数字然后我在调试时,输入其他字符,比如字母什么的,它会弹出一个错误气球“不能接受的字符”,我不想要这个东西,请问怎么屏蔽??或者改写也可以。

解决方案 »

  1.   

    BOOL CXXXDlg::PreTranslateMessage(MSG* pMsg) 
    {
        // TODO: Add your specialized code here and/or call the base class
        CEdit* m_edit = (CEdit*)GetDlgItem(IDC_EDIT1);
        if((GetFocus()==m_edit) && (pMsg->message == WM_CHAR))
        {
            if(pMsg->wParam>'9' || pMsg->wParam<'0') 
            {
                    return 0;        }
        }
        return CDialog::PreTranslateMessage(pMsg);
    }
      

  2.   

    这么改的话,是不是不需要设置edit的属性为NUMBER了?
      

  3.   


    把NUMBER属性去掉,那么字母什么的,就能输入进去了,但是我只要输入数字。
      

  4.   

    取消对Number的设置。
    再在PreTranslateMessage()中进行处理,或者处理的控件的WM_KEYDOWN或WM_KEYUP消息。
      

  5.   


    我知道了,把edit框的number去掉,然后你那个返回值错误了,返回的应该是true!这样字母就不会输入进去,也不会有消息。但是新的问题又出现了,删除不能用了!
      

  6.   


    你去掉NUMBER的属性,然后用上面那段代码,可以输入非数字的字符吗?
      

  7.   


    BOOL CMyEdit::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class

    if(pMsg->message == WM_CHAR)    
    {
    if('.' == pMsg->wParam)
    {
    if(!m_isDotExist)
    {
    m_isDotExist = TRUE;
    return CEdit::PreTranslateMessage(pMsg);
    }
    else
    {
    pMsg->wParam = 0;
    return FALSE;
    }
    } if((pMsg->wParam < '0' || pMsg->wParam >'9'))
    {                
    pMsg->wParam = 0;
    return FALSE;
    }
    } return CEdit::PreTranslateMessage(pMsg);
    }