您的位置:首页 > 其它

MFC控件edit封装,只能输入数字、负号、小数点

2016-12-08 16:19 281 查看
修改属性、或者只简单过滤字符串不怎么好用,重新封装的MFC edit控件,可以根据自己的需求继续优化封装。具体看代码:

功能介绍:

1、只能响应输入数字、负号、小数点。以及backspace键。

2、[b]只能输入一个负号、小数点。[/b]

3、[b]小数点不能在负号后面。[/b]

4、负号只能够在第一位输入。

5、可设置输入的长度(包括负号和小数点)



NumberEdit.h 

/**************************************************************************
*  @Copyright (c) 2016, QGM, All rights reserved.

*  @file     : NumberEdit.h
*  @version  : ver 1.0

*  @author   : 小光
*  @date     : 2016/12/8 16:01:23
*  @describe : 对edit控件进行了封装优化,只能够输入数字正负浮点数
*  @modify   : 无

**************************************************************************/
#pragma once

// CNumberEdit
class CNumberEdit : public CEdit
{
DECLARE_DYNAMIC(CNumberEdit)

public:
CNumberEdit();
virtual ~CNumberEdit();

protected:
DECLARE_MESSAGE_MAP()

virtual BOOL PreTranslateMessage(MSG* pMsg);

/**
* @describe:   用于判断当前Edit文本中是否包含某个字符
* @param[in]:  nChar 要检查的字符
* @return:     BOOL  TRUE:已存在; FALSE:不存在
*/
BOOL CheckUnique(char nChar);

private:
int  m_countNum ;	//保存输入的数字长度 不包括负号和小数点

int	 m_precision;	//限制输入的数字长度,包括-和小数点

public:

/**
* @describe:   设置输入长度(包括负号、小数点)
* @param[in]:  Precision	输入长度
* @return:     无
*/
void SetPrecision(int precision);
};


NumberEdit.cpp 

// NumberEdit.cpp : implementation file
//

#include "stdafx.h"
#include "NumberEdit.h"

// CNumberEdit

IMPLEMENT_DYNAMIC(CNumberEdit, CEdit)

CNumberEdit::CNumberEdit()
{
//默认精度可以输入6个字符,包括 - 和 .
m_precision = 6;
}

CNumberEdit::~CNumberEdit()
{
}

BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
END_MESSAGE_MAP()

// CNumberEdit message handlers

BOOL CNumberEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
CString str;
int nPos = 0;

if (pMsg->message == WM_KEYDOWN)
{
//截获 Delete按键 ,不允许用此键删除
if (46 == pMsg->wParam)
{
return TRUE;
}
}

if( pMsg->message == WM_CHAR )
{
//获取当前输入前已经输入的数字字符个数
GetWindowText(str);
nPos = str.GetLength();

//如果小于等于0则刚开始没有字符
if(nPos <= 0)
{
m_countNum = 0;
}

//获取光标上次的位置
int  curPosion = LOWORD(GetSel());

int startChar = 0;
int endChar = 0;
GetSel(startChar,endChar);				//获取选中的字符位置
int deleteNum = endChar - startChar;

//Backspace键及Delete键
if((8==pMsg->wParam) || (127==pMsg->wParam))
{

//如果光标不在首位置,而且在字符长度大于1,则可以按backspace键
if (8==pMsg->wParam && curPosion!=0 && nPos>1)
{
if (deleteNum!=0)
{
m_countNum = m_countNum - deleteNum;
}
else
{
m_countNum --;
}
return FALSE;
}

//无法在此获得delete键消息
//如果光标不在最后,且按下delete键时,后面有字符可以按Delete键 ,
//if (127==pMsg->wParam && curPosion!=nPos && curPosion<nPos)
//{
//	if (deleteNum!=0)
//	{
//		m_countNum = m_countNum - deleteNum;
//	}
//	else
//	{
//		m_countNum --;
//	}
//	return FALSE;
//}

//其他情况直接return
return FALSE;
}

//Enturn键
if(pMsg->wParam == VK_RETURN)
{
GetFocus()->UpdateData(TRUE);
return FALSE;//为母窗口保留处理,主要为焦点设置
}

//只允许输入一个小数点
if((46 == pMsg->wParam) && CheckUnique(46))
{
return TRUE;
}

//当前字符是’.’ 及 当前光标在第一位及当前还没有’-’,才可以输入-
if((46 == pMsg->wParam) && !CheckUnique(46))
{
if(curPosion == 0 || curPosion== m_precision)
{
return TRUE;
}

if(CheckUnique(45) && curPosion ==1)	//小数点不允许在负号后面
{
return TRUE;
}

return CEdit::PreTranslateMessage(pMsg);; //则接受这个消息,实现输入’.’
}

//只允许输入一个’-’
if(((45 == pMsg->wParam) && CheckUnique(45)))
{
return TRUE;
}

//当前字符是’-’ 及 当前光标在第一位及当前还没有’-’,才可以输入-
if((45 == pMsg->wParam) && !CheckUnique(45))
{
if (curPosion!=0)		//光标不在第一位则不能输入 -
{
return TRUE;
}

return CEdit::PreTranslateMessage(pMsg); //则接受这个消息,实现输入’-’
}

//只允许输入负号,数字,点号。其它不处理直接返回
if(45 != pMsg->wParam && !(pMsg->wParam >= 48 && pMsg->wParam <= 57) && 46 != pMsg->wParam )
{
return TRUE;
}

//只能输入6个字符包括(-  .)
if(nPos >= m_precision)
{
return TRUE;
}

//只能连续输入多少个数字
if(pMsg->wParam >= 48 && pMsg->wParam <= 57)
{
m_countNum ++;
}

}

return CEdit::PreTranslateMessage(pMsg);
}

BOOL CNumberEdit::CheckUnique(char nChar)
{
CString str;
int nPos = 0;
GetWindowText(str);
nPos = str.Find(nChar);

return (nPos >=0 )?TRUE:FALSE;
}

void CNumberEdit::SetPrecision(int precision)
{
m_precision = precision;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐