282 lines
5.2 KiB
C++
282 lines
5.2 KiB
C++
// floatedit.cpp : implementation file
|
|
//
|
|
|
|
#include "stdafx.h"
|
|
#include "geomative.h"
|
|
#include "floatedit.h"
|
|
|
|
#ifdef _DEBUG
|
|
#define new DEBUG_NEW
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[] = __FILE__;
|
|
#endif
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// CFloatEdit
|
|
|
|
CFloatEdit::CFloatEdit()
|
|
{
|
|
m_iIntLimitLen = 4;
|
|
m_iDecLimitLen = 2;
|
|
|
|
m_bIsMinus = FALSE;
|
|
}
|
|
|
|
CFloatEdit::~CFloatEdit()
|
|
{
|
|
}
|
|
|
|
|
|
BEGIN_MESSAGE_MAP(CFloatEdit, CEdit)
|
|
//{{AFX_MSG_MAP(CFloatEdit)
|
|
ON_WM_CHAR()
|
|
ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
|
|
ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
|
|
ON_WM_KEYDOWN()
|
|
//}}AFX_MSG_MAP
|
|
END_MESSAGE_MAP()
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// CFloatEdit message handlers
|
|
|
|
void CFloatEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
|
|
{
|
|
// TODO: Add your message handler code here and/or call default
|
|
CString szText = _T("");
|
|
int iLen = 0;
|
|
int iIntLen = 0;
|
|
int iDecLen = 0;
|
|
|
|
int iStartChar = 0;
|
|
int iEndChar = 0;
|
|
|
|
int iMinus = 0;
|
|
|
|
GetWindowText(szText);
|
|
iLen = szText.GetLength();
|
|
|
|
GetSel(iStartChar, iEndChar);
|
|
|
|
if ((FALSE == m_bIsMinus) && ('-' == nChar))
|
|
{
|
|
nChar = 0;
|
|
}
|
|
|
|
if((nChar >= '0' && nChar <= '9' ) || ('.' == nChar) || ('-' == nChar))
|
|
{
|
|
iMinus = 0;
|
|
|
|
if ((szText.Left(1) != _T("-")) && ((iStartChar == iEndChar) && (0 == iStartChar)) && ('-' == nChar))
|
|
{
|
|
szText.Insert(0, (TCHAR)nChar);
|
|
SetWindowText(szText);
|
|
SetSel(1, 1);
|
|
}
|
|
else
|
|
{
|
|
if ((szText.Left(1) == _T("-")) && ('-' == nChar))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (szText.Find("-") == 0)
|
|
{
|
|
iMinus = 1;
|
|
}
|
|
|
|
if (szText.Find('.') < 0)
|
|
{
|
|
if ('.' == nChar)
|
|
{
|
|
if (iLen > 0)
|
|
{
|
|
szText = szText + '.';
|
|
SetWindowText(szText);
|
|
SetSel(iLen, ++iLen); //依据C++参数传递顺序,先执行++iLen,此时iLen等于iLen+1
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (iStartChar == iEndChar)
|
|
{
|
|
if (iLen < (m_iIntLimitLen + iMinus))
|
|
{
|
|
if (nChar != '-')
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
iIntLen = szText.Find('.') - iMinus;
|
|
iDecLen = iLen - szText.Find('.') - 1;
|
|
|
|
if ('.' != nChar)
|
|
{
|
|
/*
|
|
if ((iStartChar == iEndChar) && (iStartChar > iIntLen) && (iDecLen < m_iDecLimitLen)) //控制小数位长度
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
|
|
if ((iStartChar == iEndChar) && (iStartChar <= iIntLen) && (iIntLen < m_iIntLimitLen)) //控制整数位长度
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
*/
|
|
if (iStartChar == iEndChar)
|
|
{
|
|
if (iStartChar > iIntLen)
|
|
{
|
|
if (iDecLen < m_iDecLimitLen) //控制小数位长度
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (iIntLen < (m_iIntLimitLen)) //控制整数位长度
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
else if (0x0008 == nChar) //VK_BACK == 0x0008
|
|
{
|
|
if (szText.Find('.') >= 0)
|
|
{
|
|
iIntLen = szText.Find('.');
|
|
iDecLen = iLen - szText.Find('.') - 1;
|
|
|
|
if ((iIntLen + 1) == iStartChar)
|
|
{
|
|
szText = szText.Left(iIntLen);
|
|
SetWindowText(szText);
|
|
|
|
iLen = szText.GetLength();
|
|
SetSel(iLen, ++iLen);
|
|
return;
|
|
}
|
|
}
|
|
CEdit::OnChar(nChar, nRepCnt, nFlags);
|
|
}
|
|
}
|
|
|
|
void CFloatEdit::OnKillfocus()
|
|
{
|
|
// TODO: Add your control notification handler code here
|
|
CString szText = _T("");
|
|
int iLen = (int)VAL_ZERO;
|
|
int iIntLen = (int)VAL_ZERO;
|
|
|
|
GetWindowText(szText);
|
|
|
|
if ((szText.GetLength() > (int)VAL_ZERO) && (float)VAL_ZERO == atof(szText))
|
|
{
|
|
szText = _T("0");
|
|
// SetWindowText(szText);
|
|
// return;
|
|
}
|
|
else
|
|
{
|
|
szText.TrimLeft('0');
|
|
// szText.TrimRight('0');
|
|
}
|
|
|
|
iLen = szText.GetLength();
|
|
|
|
if ((iLen - 1) == szText.Find('.'))
|
|
{
|
|
szText.Delete(--iLen, 1);
|
|
}
|
|
|
|
iIntLen = szText.Find('.');
|
|
if (0 == iIntLen)
|
|
{
|
|
szText = '0' + szText;
|
|
SetSel(iLen, ++iLen);
|
|
}
|
|
SetWindowText(szText);
|
|
}
|
|
|
|
void CFloatEdit::SetIntLimitLen(int iIntLimitLen)
|
|
{
|
|
this->m_iIntLimitLen = iIntLimitLen;
|
|
}
|
|
|
|
void CFloatEdit::SetDecLimitLen(int iDecLimitLen)
|
|
{
|
|
this->m_iDecLimitLen = iDecLimitLen;
|
|
}
|
|
|
|
void CFloatEdit::SetIsMinus(BOOL bIsMinus)
|
|
{
|
|
this->m_bIsMinus = bIsMinus;
|
|
}
|
|
|
|
void CFloatEdit::OnSetfocus()
|
|
{
|
|
// TODO: Add your control notification handler code here
|
|
CString szText = _T("");
|
|
int iLen = 0;
|
|
|
|
GetWindowText(szText);
|
|
iLen = szText.GetLength();
|
|
SetSel(iLen, ++iLen);
|
|
}
|
|
|
|
void CFloatEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
|
|
{
|
|
// TODO: Add your message handler code here and/or call default
|
|
|
|
CString szText = _T("");
|
|
int iLen = 0;
|
|
int iIntLen = 0;
|
|
int iDecLen = 0;
|
|
|
|
int iStartChar = 0;
|
|
int iEndChar = 0;
|
|
|
|
GetWindowText(szText);
|
|
iLen = szText.GetLength();
|
|
|
|
GetSel(iStartChar, iEndChar);
|
|
if (0x002E == nChar) //VK_DELETE == 0x002E
|
|
{
|
|
if (szText.Find('.') >= 0)
|
|
{
|
|
iIntLen = szText.Find('.');
|
|
iDecLen = iLen - szText.Find('.') - 1;
|
|
|
|
if (iIntLen == iStartChar)
|
|
{
|
|
szText = szText.Left(iIntLen);
|
|
SetWindowText(szText);
|
|
|
|
iLen = szText.GetLength();
|
|
SetSel(iLen, ++iLen);
|
|
return;
|
|
}
|
|
}
|
|
|
|
}
|
|
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
|
|
}
|