The day before yesterday,I wrote some codes about how to use Edit control and Spin control together in order to handle some addition or decrement a float data,which will be passed from another function.Beacuse float data always changes its number automatically,so you can't add one or sub one simply.Here,you should think about how to use another way that enable to add a float data? I focused on it a long time and I dealt with it finally.I put my program to share with you.The next content is my program.
First of all,you should inherit CEdit class from base class (only CEdit) like CXXXCtrl something like that...please read the next code:
class CNumberEditCtrl : public CEdit
{
DECLARE_DYNAMIC(CNumberEditCtrl)
public:
CNumberEditCtrl();
virtual ~CNumberEditCtrl();
void AddUpdatePoints( double nPoints);
void SubUpdatePoints(double nPoints);
void LessThanZeroHandler();
void HandleChangeData(CString strData);
protected:
DECLARE_MESSAGE_MAP()
public:
CToolTipCtrl m_ToolTip;
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//保留两个消息处理函数
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
void UpdateEdit(CString strOffer);
double GetDataFEdit(CString strData);
virtual BOOL PreTranslateMessage(MSG* pMsg);
void decodeCString(CString source, CStringArray& dest, char division);//分割字符串
};
So the above code is very simple.maybe you will never read them and understand what they wanna do!next,you will see how to realize them function.please look at them like this:
(attention:just only main function like OnChar,AddUpdatePoints( double nPoints))
void CNumberEditCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( nChar >= 48 && nChar <= 57 || nChar == 46)
{
int nStart;
int nEnd;
GetSel( nStart, nEnd );
CString text;
CString buffer;
char strArray[MAX];
char strLArray[MAX];
ZeroMemory(strArray,sizeof(strArray));
ZeroMemory(strLArray,sizeof(strLArray));
GetWindowText( text );
int len = text.GetLength();
int nPointPos = text.Find( '.' );
text.Insert( nStart, nChar );//在nStart位置插入新的字符
if( nPointPos != -1)
{
if (nStart<nPointPos)//判断插入的位置小于nPointPos,用插入的字符替代原来的字符
{
//text.Remove(text.GetAt(nStart+1));//这里把所有相同的字符全部移走了
//text.Remove('.');
//text.Insert( nPointPos, '.');
text.Delete(nStart+1,1);//删除第nStart+1个字符
buffer = text.Left(len);
SetWindowText(buffer);
nStart ++;
SetSel( nStart, nStart );
}
else if (nStart==nPointPos)//当插入的位置是在小数点前面时,要截取小数点后面的字符串
{
if (nChar != 46)
{
for (int i=0; i<=nStart; i++)
{
strArray[i] = text.GetAt(i);
}
for (int i=nStart+1; i<text.GetLength(); i++)
{
strLArray[i-nStart-1] = text.GetAt(i);
}
CString strTemp1(strArray);
CString strTemp2(strLArray);
CString textTemp = strTemp1 + strTemp2;
buffer = textTemp.Left(textTemp.GetLength());
SetWindowText(buffer);
nStart++;
SetSel(nStart,nStart);
}
else if (nChar == 46)
{
text.Delete(nStart+1,1);
buffer = text.Left(text.GetLength());
SetWindowText(buffer);
nStart++;
SetSel(nStart,nStart);
}
else
{
return;
}
}
else //判断插入的位置大于nPointPos
{
text.Delete(nStart+1,1);
buffer = text.Left(text.GetLength());
SetWindowText(buffer);
nStart ++;
SetSel(nStart,nStart);
}
}
}
else if( nChar == VK_BACK )
{
int nStart;
int nEnd;
GetSel( nStart, nEnd );
if( nEnd == 0 )
return;
CString text;
CString buffer;
GetWindowText( buffer );
int nPointPos = buffer.Find( '.' );
CEdit::OnChar(nChar, nRepCnt, nFlags);
GetWindowText( text );
if( nEnd > nStart )
{
for( int i = nStart; i < nEnd ; i ++ )
{
if( i == nPointPos )
text.Insert( i , '.' );
else
text.Insert( i, '0' );
}
text.TrimLeft( _T("0") );
SetWindowText( text );
SetSel( nStart, nStart );
}
else if( nStart == nEnd )
{
if( nPointPos == nStart - 1 )
ReplaceSel( _T(".") );
else
ReplaceSel( _T("0") );
nStart -- ;
if( nStart < 0 )
nStart = 0;
SetSel( nStart, nStart);
}
}
else
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
void CNumberEditCtrl::AddUpdatePoints( double nPoints )// add a float data to Edit Control
{
int m_Index;
char strArray[MAX];
char strTemp[MAX]; //定义一个临时数组
ZeroMemory(strArray,sizeof(strArray));
ZeroMemory(strTemp,sizeof(strTemp));
double temp = 0.0;
CString strOffer; //用来存储浮点转换成CString的临时变量
CString buffer;
GetWindowText(buffer); //得到编辑框的内容
m_Index = buffer.GetLength();
if (!buffer.IsEmpty())
{
for (int i=0; i<m_Index; i++)
{
strTemp[i] = buffer.GetAt(i);
}
temp = atof(strTemp);
temp = temp + nPoints;
strOffer.Format(_T("%.10f"),temp); //把浮点数转换成CString 类型
//确定小数点后的有效数字
if (strOffer.GetAt(strOffer.GetLength()-1)!='0')
{
strOffer = strOffer + '0';
buffer = strOffer;
SetWindowText(buffer);
}
else
{
int flag = 0; //标志位
for (int i=strOffer.GetLength()-1; i>=0; i--)
{
if (strOffer.GetAt(i)!='0')
{
flag = i + 2; //记下倒数第一个不等于零的数的后一位零的位置
break;
}
}
for (int j=0; j<flag; j++)
{
strArray[j] = strOffer.GetAt(j);
}
CString IsCharChange(strArray); //IsCharChange用来存储字符转换成CString 的临时变量
buffer = IsCharChange;
SetWindowText(buffer); //保存修改
}
}
}
onChar and AddUpdatePoints are core of algorithm.
本文介绍了一种使用编辑控件处理浮点数加减的方法。通过自定义CNumberEditCtrl类实现对浮点数的有效输入及更新。文章详细解释了如何通过OnChar和AddUpdatePoints等函数确保数据正确性和用户体验。
707

被折叠的 条评论
为什么被折叠?



