类定义
#if !defined(AFX_MYEDIT_H)
#define AFX_MYEDIT_H
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
//
#include "afxcmn.h"
class CMyEdit : public CEdit
{
public:
CMyEdit();
~CMyEdit();
DECLARE_MESSAGE_MAP()
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnKillFocus(CWnd* pNewWnd);
};
#endif;
实现函数
#include "stdafx.h"
#include "MyEdit.h"
CMyEdit::CMyEdit()
{
}
CMyEdit::~CMyEdit()
{
}
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_WM_CHAR()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
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
{
CString str;
// 获取原来编辑框中的字符串
GetWindowText(str);
//若原来的字符串中已经有一个负号,则不将负号输入,保证了最多只能输入一个负号
if(str.Find('-') != -1)
return;
int nSource,nDestination;
GetSel(nSource,nDestination);
if(nSource == 0 &&
(nDestination == str.GetLength() // 此时选择了全部的内容
|| nSource == 0 && nDestination == 0 //光标在第首个字符
|| nSource == 0 && nDestination == 1))//选择了第一个字符
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
}
// 除了小数点和负号,还允许输入数字,Backspace,Delete
else if((nChar >= '0' && nChar <= '9') || (nChar == 0x08) || (nChar == 0x10))
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
// 其它的键,都不响应
}
void CMyEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
CString str;
// 获取原来编辑框中的字符串
GetWindowText(str);
if(str.Find('.') == -1)
str.Format("%.2f", _ttof(str)); //自定义显示几位小数
SetWindowText(str);
// TODO: 在此处添加消息处理程序代码
}