Duilib Edit编辑框禁止输入中文的方法

转载:http://www.myexception.cn/vc-mfc/300749.html

编辑框是供用户输入的,但有时候我们要限制用户输入的内容,比如我们不让用户输入中文,只能输入字符和数字,因此要对用户输入的内容进行过滤。

Duilib中的Edit是原生的窗口,当用户输入时,编辑框的内容被用户改变了,会触发EN_CHANGE事件

看Duilib源码

所以在我们自己的窗口类里处理 _T("textchanged") 消息类型

    else if (msg.sType == _T("textchanged"))
    {
        if ( _tcscmp(m_pEdit->GetText(),m_pEdit->GetSrcTipValue()) != 0 &&!m_pEdit->GetText().IsEmpty())
        {
            CString strValue = m_pEdit->GetText();

            for ( int nIndex = 0; nIndex < strValue.GetLength() ; nIndex++ )
            {
             if( strValue.GetAt( nIndex ) >=128 || strValue.GetAt( nIndex ) < 0 )
                {
//ASCII码0~127是数字和字符,所以大于128的小于0的都过滤掉 strValue
= strValue.Left( nIndex ) + strValue.Right( strValue.GetLength() - nIndex - 1 );//包含中文的字符过滤掉 m_pEdit->SetText(strValue); m_pEdit->SetSel(strValue.GetLength(),strValue.GetLength());//重设给光标设置位置 } } } }

看效果

原文地址:https://www.cnblogs.com/chechen/p/7374676.html