16,551
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "NumberEdit.h"
#include "float.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNumberEdit
CNumberEdit::CNumberEdit()
{
m_nMinValue = 0;
m_nMaxValue = 5000;
m_nDelta = FLT_ROUNDS;
}
CNumberEdit::~CNumberEdit()
{
}
BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
//{{AFX_MSG_MAP(CNumberEdit)
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNumberEdit message handlers
//键盘按下
void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
BOOL bRet = FALSE;
CString str;
GetWindowText(str);
switch(nChar)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case VK_BACK:
bRet = TRUE;
break;
case '.':
if(str.Find('.')<0)
bRet = TRUE;
break;
default:
break;
}
if(bRet)
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
//获取输出的范围
void CNumberEdit::GetRange(int & min, int & max) const
{
min = m_nMinValue;
max = m_nMaxValue;
}
//设置输出的范围
void CNumberEdit::SetRange(int min, int max)
{
m_nMinValue = min;
m_nMaxValue = max;
}
//获取步进值
int CNumberEdit::GetDelta()
{
return m_nDelta;
}
//设置步进值
void CNumberEdit::SetDelta(int nDelta)
{
m_nDelta = nDelta;
}
//获取小数
int CNumberEdit::GetDecimal()const
{
int n = 0;
CString str;
GetWindowText(str);
n = int(atof(str)*10); //转换成整数
return (n);
}
//设置小数
BOOL CNumberEdit::SetDecimal(int nDecimal)
{
if (nDecimal > m_nMaxValue || nDecimal < m_nMinValue)
return FALSE;
CString str;
str.Format("%d.%d",nDecimal/10,nDecimal%10);
SetWindowText(str);
return TRUE;
}
//改变小数时
void CNumberEdit::ChangeDecimal(int step)
{
int n = GetDecimal() + step * m_nDelta;
if(n > m_nMaxValue)
n = m_nMaxValue;
else if(n < m_nMinValue)
n = m_nMinValue;
SetDecimal(n);
}
/////////////////////////////这里是头文件中需要增加的函数//////////////////////
// Implementation
public:
virtual ~CNumberEdit();
virtual int GetDelta();
virtual void SetDelta(int nDelta);
virtual void GetRange(int &min, int &max)const;
virtual void SetRange(int min, int max);
virtual void ChangeDecimal(int step);
virtual BOOL SetDecimal(int nDecimal);
virtual int GetDecimal()const;
// Generated message map functions
protected:
int m_nDelta, m_nMinValue, m_nMaxValue;
//{{AFX_MSG(CNumberEdit)
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
BOOL CMy22Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_edit.SetRange(0,200); //范围
m_edit.SetDelta(1); //步进
m_edit.SetDecimal(0); //初始值
return TRUE; // return TRUE unless you set the focus to a control
}
void CMy22Dlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
m_edit.ChangeDecimal(-pNMUpDown->iDelta);
m_edit.SetDecimal(m_edit.GetDecimal());
*pResult = 0;
}
void CMy22Dlg::OnKillfocusEdit1()
{
// TODO: Add your control notification handler code here
m_edit.ChangeDecimal(0);
m_edit.SetDecimal(m_edit.GetDecimal());
}