CSpinButtonCtrl控件处理小数的问题

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

问题来源:

上下控件一般是和一个输入数字的Edit控件一起使用的,而且一般数字应该是整数,最近在项目开发中要求Edit中能输入小数。这样原来的上下控件就要做一些改动了。比如当前的Edit框中是4.3按了上下控件后应该为4和5。

改动思路:

写一个继承CSpinButtonCtrl的类CSpinButtonCtrl,响应OnLButtonDown,在该函数中由与上下控件相关编辑控件中的数字来重新设置上下控件的基点。

OnLButtonDown函数:

void CMySpinButtonCtrl::OnLButtonDown(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

int nRow;

int nUpper;

CMySpinButtonCtrl::GetRange(nRow, nUpper);

/*

*判断编辑框中的输入是否合法

*/

CString strNum;

CMySpinButtonCtrl::GetBuddy()->GetWindowText(strNum);

if ( g_IsNumber(strNum) ==FALSE )

{

MessageBox("请输入合法的数字");

return;

}

else

{

CRect Rect;

CMySpinButtonCtrl::GetWindowRect (&Rect);

if( strNum.Find('.') > 0 )

{

int nDotpos = strNum.Find('.');

int nLength = strNum.GetLength();

strNum = strNum.Left(nDotpos+1);

int pos = atoi(strNum);

if(point.y <((Rect.bottom-Rect.top)/2))

{

CMySpinButtonCtrl::SetPos(pos);

}

else

{

CMySpinButtonCtrl::SetPos(pos + 1);

}

}

else

{

int pos = atoi(strNum);

CMySpinButtonCtrl::SetPos(pos);

}

}

CSpinButtonCtrl::OnLButtonDown(nFlags, point);

}

//其中g_IsNumber是一个判断某个字符串是否是数字(可以是小数)的函数

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航