问题来源: 上下控件一般是和一个输入数字的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是一个判断某个字符串是否是数字(可以是小数)的函数 
|