📄 numedit.cpp
字号:
// NumEdit.cpp : implementation file
//
#include "stdafx.h"
#include "NumEdit.h"
#include "Float.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNumEdit
IMPLEMENT_DYNAMIC(CNumEdit, CEdit)
CNumEdit::CNumEdit()
{
m_NumberOfNumberAfterPoint = 0;
m_Verbose = FALSE;
m_MinValue = MINRANGE;
m_MaxValue = MAXRANGE;
m_Delta = FLT_ROUNDS;
}
CNumEdit::~CNumEdit()
{
}
BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
//{{AFX_MSG_MAP(CNumEdit)
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNumEdit message handlers
NUMSTYLE CNumEdit::GetValue() const
{
float f = 0.0f;
if (IsValid() == VALID)
{
CString str;
GetWindowText(str);
swscanf((TCHAR *)(LPCTSTR)str, _T("%f"), &f);
}
return (NUMSTYLE)f;
}
BOOL CNumEdit::SetValue(NUMSTYLE val)
{
if (val > m_MaxValue || val < m_MinValue)
return FALSE;
CString str, s;
str.Format(ConstructFormat(s, val), val);
SetWindowText(str);
return TRUE;
}
int CNumEdit::IsValid() const
{
CString str;
GetWindowText(str);
int res = VALID;
float f;
TCHAR lp[10];
if (swscanf((TCHAR *)(LPCTSTR)str, _T("%f%s"), &f, lp) != 1)
res = INVALID_CHAR;
if (f > m_MaxValue || f < m_MinValue)
res = OUT_OF_RANGE;
if (m_Verbose && res != VALID)
{
str.Empty();
if (res & OUT_OF_RANGE)
str += _T("Given value is out of range.\n");
if (res & INVALID_CHAR)
str += _T("Characters must be a number.\n");
AfxMessageBox(str, MB_OK | MB_ICONSTOP);
}
return res;
}
int CNumEdit::IsValid(const CString &str) const
{
int res = VALID;
NUMSTYLE f;
char lp[10];
if (swscanf((TCHAR *)(LPCTSTR)str, _T("%f%s"), &f, lp) != 1)
res = INVALID_CHAR;
if (f > m_MaxValue && f < m_MinValue)
res = OUT_OF_RANGE;
if (m_Verbose && res != VALID)
{
CString msg;
msg.Empty();
if (res & OUT_OF_RANGE)
msg += _T("Given value is out of range.\n");
if (res & INVALID_CHAR)
msg += _T("Characters must be a number.\n");
AfxMessageBox(str, MB_OK | MB_ICONSTOP);
}
return res;
}
void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == ' ')
return;
NUMSTYLE oldValue = GetValue();
CEdit::OnChar(nChar, nRepCnt, nFlags);
if (IsValid() != VALID)
{
SetValue(oldValue);
SetSel(0, -1);
MSG msg;
while (::PeekMessage(&msg, m_hWnd, WM_CHAR, WM_CHAR, PM_REMOVE));
}
else
{
CString s;
ConstructFormat(s, GetValue());
}
}
BOOL CNumEdit::Verbose() const
{
return m_Verbose;
}
void CNumEdit::Verbose(BOOL v)
{
m_Verbose = v;
}
void CNumEdit::SetRange(NUMSTYLE min, NUMSTYLE max)
{
m_MinValue = min;
m_MaxValue = max;
}
void CNumEdit::GetRange(NUMSTYLE min, NUMSTYLE max) const
{
min = m_MinValue;
max = m_MaxValue;
}
void CNumEdit::SetDelta(NUMSTYLE delta)
{
m_Delta = delta;
}
NUMSTYLE CNumEdit::GetDelta()
{
return m_Delta;
}
void CNumEdit::ChangeAmount(int step)
{
NUMSTYLE f = GetValue() + step * m_Delta;
if (f > m_MaxValue)
f = m_MaxValue;
else if (f < m_MinValue)
f = m_MinValue;
SetValue(f);
}
CString& CNumEdit::ConstructFormat(CString &str, NUMSTYLE num)
{
str.Format(_T("%d"),num);
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -