// EditInput.cpp : 实现文件 // #include "stdafx.h" #include "CH91PayloadSoftware.h" #include "EditInput.h" // CEditInput IMPLEMENT_DYNAMIC(CEditInput, CEdit) CEditInput::CEditInput() { } CEditInput::~CEditInput() { } BEGIN_MESSAGE_MAP(CEditInput, CEdit) ON_WM_CHAR() END_MESSAGE_MAP() void CEditInput::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { // 保证小数点最多只能出现一次 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 = nSource时表示未选中 //光标位于最前方并且str中已经有'-'不操作 if (str[0] == '-' && nDestination == 0) { } else CEdit::OnChar(nChar, nRepCnt, nFlags); } else { } } } //小数点,负号,还应该允许输入数字,backspace, delete else if( (nChar>='0' && nChar<='9') || (nChar == 0x08) || (nChar == 0x10) ) { CEdit::OnChar(nChar, nRepCnt, nFlags); } //其余不响应 else { } //CEdit::OnChar(nChar, nRepCnt, nFlags); }