MFC实现Edit输入限制(只允许输入数字,负号和小数点)

  1)添加个C++类 eg. class Dot:public CEdit

 

  2)给这个类添加onChar()消息

 

  afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);

 

  3)*.cpp中

void Dot::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
 // TODO: Add your message handler code here and/or call default
 // 保证小数点最多只能出现一次
 if(nChar==’.’)
 {
    CString str;
    // 获取原来编辑框中的字符串
     GetWindowText(str);
     //若原来的字符串中已经有一个小数点,则不将小数点输入,保证了最多只能输入一个小数点
    if(str.Find(‘.’)!=-1)
    {
    }
    // 否则就输入这个小数点
   else
   {
      CEdit::OnChar(nChar, nRepCnt, nFlags); 
    }
 }
 // 保证负号只能出现一次,并且只能出现在第一个字符
 else if(nChar==’-‘)
 {
    CString str;
  GetWindowText(str);
  // 还没有输入任何字符串
  if(str.IsEmpty())
  {
   CEdit::OnChar(nChar, nRepCnt, nFlags); 
  }
  else
  {
   int nSource,nDestination;
   GetSel(nSource,nDestination);
   // 此时选择了全部的内容
   if(nSource==0&&nDestination==str.GetLength())
   {
    CEdit::OnChar(nChar, nRepCnt, nFlags); 
   }
   else
   {
   
   
 }
 // 除了小数点和负号,还允许输入数字,Backspace,Delete
 else if((nChar>=’0’ && nChar<=’9’)||(nChar==0x08)||(nChar==0x10))
 {
  CEdit::OnChar(nChar, nRepCnt, nFlags); 
 }
 // 其它的键,都不响应
 else
 {
 }
}

 

4)在*Dlg.h加上

—-》#include”Dot.h”

—-》Dot  m_s;

 

5)在*Dlg.cpp加上

—–》DDX_Control(pDX, IDC_EDIT1, m_s);

 





或者


  1. BOOL CWeiXinQ::PreTranslateMessage(MSG* pMsg)  
  2. {  
  3.     // TODO: 在此添加专用代码和/或调用基类  
  4.         //指定对话框只接受数字按键输入,其他符号输入无效  
  5.       
  6.     //获取控件窗口指针  
  7.     CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT1_Q_MONEY);  
  8.     CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT2_Q_CODE);  
  9.     if( (GetFocus() == pEdit1 ||GetFocus() == pEdit2) && (pMsg->message == WM_CHAR)  )  
  10.     {  
  11.         //只允许输入数字和小数点“.”  
  12.         if((pMsg->wParam <= ‘9’ && pMsg->wParam >= ‘0’) || pMsg->wParam == ‘.’)   
  13.         {  
  14.             //金额输入框只允许输入一个小数点  
  15.             if(pMsg->wParam == ‘.’)  
  16.             {  
  17.                 CString str;  
  18.                 int nPos = 0;  
  19.                 GetDlgItemText(IDC_EDIT1_Q_MONEY, str); // 获取edit中文本  
  20.                 nPos = str.Find(’.’);   // 查找.的位置  
  21.                 if(nPos >= 0)  
  22.                 {  
  23.                     return 1;  
  24.                 }  
  25.             }  
  26.               
  27.             return 0;  
  28.               
  29.         }  
  30.         else if(pMsg->wParam == 0x08 || pMsg->wParam == 0x10)          //接受Backspace和delete键  
  31.         {  
  32.                 return 0;  
  33.         }  
  34.         else  
  35.         {  
  36.             //响应标签页切换的快捷键  
  37.             switch(pMsg->wParam)  
  38.             {  
  39.             case ‘q’:  
  40.             case ‘Q’:  
  41.             case ‘w’:  
  42.             case ‘W’:  
  43.             case ‘e’:  
  44.             case ‘E’:  
  45.             case ‘r’:  
  46.             case ‘R’:  
  47.             case ‘t’:  
  48.             case ‘T’:  
  49.             case ‘y’:  
  50.             case ‘Y’:  
  51.             case ‘u’:  
  52.             case ‘U’:  
  53.             case ‘i’:  
  54.             case ‘I’:  
  55.             case ‘o’:  
  56.             case ‘O’:  
  57.                 CWnd *pParent = GetParent();  
  58.                 pParent->SetFocus();  
  59.             }  
  60.             return 1;  
  61.         }  
  62.   
  63.     }  
  64.   
  65.     return CDialogEx::PreTranslateMessage(pMsg);  
  66. }  
BOOL CWeiXinQ::PreTranslateMessage(MSG* pMsg)
{
    // TODO: 在此添加专用代码和/或调用基类
        //指定对话框只接受数字按键输入,其他符号输入无效

    //获取控件窗口指针
    CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT1_Q_MONEY);
    CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT2_Q_CODE);
    if( (GetFocus() == pEdit1 ||GetFocus() == pEdit2) && (pMsg->message == WM_CHAR)  )
    {
        //只允许输入数字和小数点“.”
        if((pMsg->wParam <= '9' && pMsg->wParam >= '0') || pMsg->wParam == '.') 
        {
            //金额输入框只允许输入一个小数点
            if(pMsg->wParam == '.')
            {
                CString str;
                int nPos = 0;
                GetDlgItemText(IDC_EDIT1_Q_MONEY, str); // 获取edit中文本
                nPos = str.Find('.');   // 查找.的位置
                if(nPos >= 0)
                {
                    return 1;
                }
            }

            return 0;

        }
        else if(pMsg->wParam == 0x08 || pMsg->wParam == 0x10)          //接受Backspace和delete键
        {
                return 0;
        }
        else
        {
            //响应标签页切换的快捷键
            switch(pMsg->wParam)
            {
            case 'q':
            case 'Q':
            case 'w':
            case 'W':
            case 'e':
            case 'E':
            case 'r':
            case 'R':
            case 't':
            case 'T':
            case 'y':
            case 'Y':
            case 'u':
            case 'U':
            case 'i':
            case 'I':
            case 'o':
            case 'O':
                CWnd *pParent = GetParent();
                pParent->SetFocus();
            }
            return 1;
        }

    }

    return CDialogEx::PreTranslateMessage(pMsg);
}



  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值