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)
就这样吧,能够实现下面的效果
先按照图片布局添加空间,为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)
就这样吧,能够实现下面的效果
相关文章推荐
- 朱刘算法
- Hibernate操作流程
- 使用jbox弹窗,关闭子窗体并刷新父窗体
- Service学习(本地服务的两种启动方式以及跨进程服务)
- [caffe]深度学习之图像分类模型AlexNet解读
- IO系统性能之二:缓存和RAID如何提高IO
- linux 下vi进行文件编辑的常用命令1
- 数字计算的有序排列的号码出现二分法
- Web应用与Web应用防火墙之网络安全产品追述
- 思念,在天涯
- 在线编程刷题碰到的一些问题
- java保留两位小数
- rsync技术总结
- Java+jsp实现简单登录
- 深入剖析web的状态管理-Cookie
- java学习经典书籍_杂篇
- 性能测试常见问题 (性能论述)
- 【MySQL】sync_binlog innodb_flush_log_at_trx_commit 浅析
- 北京Loft投资分析
- Wilson定理证明