您的位置:首页 > 其它

MFC 利用Spin控件增减编辑框控件数值

2015-09-11 17:43 417 查看
这两天要做一个对话框对数据进行设置,里面需要用到Spin控件增减Edit button控件里面的数值,网上查找了一个方法,但是只能实现整数的增减,于是另外找了点资料,这里记录一下:



先按照图片布局添加空间,为Edit控件添加CString类型的值作为变量,为Spin添加空间变量,将Spin控件Auto Hscoll,visible,border属性设置为true

这是添加的控件变量:

private:
CString m_strEditLine;
CString m_strEditPoint;
CSpinButtonCtrl m_SpinLine;
CSpinButtonCtrl m_SpinPoint;
int m_iLeastPoint;//这个是保存Edit空间里面的值为数字
double m_dLeastDisLine;<span style="font-family: Arial, Helvetica, sans-serif;">//这个是保存Edit空间里面的值为数字</span>

然后添加下面代码:
void CDlgWFExportSet::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_strEditPoint.IsEmpty() || m_strEditLine.IsEmpty())
{
AfxMessageBox(_T("参数设置不正确!"));
return;
}
m_iLeastPoint = _tstof(m_strEditPoint);
m_dLeastDisLine = _tstof(m_strEditLine);
CDialogEx::OnOK();
}

BOOL CDlgWFExportSet::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
CEdit* editTop = (CEdit*)GetDlgItem(IDC_EDIT_LINE);
CEdit* editBottom = (CEdit*)GetDlgItem(IDC_EDIT_POINT);
if((GetFocus()==editTop || GetFocus()==editBottom) && (pMsg->message == WM_CHAR))
{
if(pMsg->wParam<='9' && pMsg->wParam>='0' || pMsg->wParam=='.' || pMsg->wParam == VK_BACK)
{
return 0;
}
else
{
return 1;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}

void CDlgWFExportSet::SetEditContent(LPNMUPDOWN pNMUpDown, CString &strEdit)
{
double dStep = 1.00;
double dCurValue = _tstof(strEdit);
if(pNMUpDown->iDelta == 1) // 如果此值为1 , 说明点击了Spin的往下箭头
{
dCurValue -= dStep;
dCurValue = dCurValue<0?0:dCurValue;
}
else if(pNMUpDown->iDelta == - 1) // 如果此值为-1 , 说明点击了Spin的往上箭头
{
dCurValue += dStep;
dCurValue = dCurValue<0?0:dCurValue;
}
strEdit.Format(_T("%.2f"), dCurValue);
}

void CDlgWFExportSet::OnDeltaposSpinPoint(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
UpdateData();
SetEditContent(pNMUpDown, m_strEditPoint);
int Pos = m_strEditPoint.Find('.');
m_strEditPoint = m_strEditPoint.Left(Pos);
UpdateData(FALSE);
}

void CDlgWFExportSet::OnDeltaposSpinLine(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
UpdateData();
SetEditContent(pNMUpDown, m_strEditLine);
UpdateData(FALSE);
}

这里注意OnDeltaposSpinPoint和OnDeltaposSpinLine的消息映射的形式:
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_POINT, &CDlgWFExportSet::OnDeltaposSpinPoint)
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_LINE, &CDlgWFExportSet::OnDeltaposSpinLine)

就这样吧,能够实现下面的效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: