⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numedit.cpp

📁 C++编程实践与技巧一书各章节的源码
💻 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

CNumEdit::CNumEdit()
{
	m_MinValue = -FLT_MAX;
	m_MaxValue = FLT_MAX;
}

CNumEdit::~CNumEdit()
{
}


BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
	//{{AFX_MSG_MAP(CNumEdit)
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNumEdit message handlers


int CNumEdit::IsValid()
{
	CString str;
	GetWindowText(str);
	int res = VALID;
	float f;
	char lp[10];
	if (sscanf(str, "%f%s", &f, lp) != 1) res = INVALID_CHAR;
	if (f > m_MaxValue && f < m_MinValue) res = OUT_OF_RANGE;
	if (res != VALID)
	{
		str.Empty();
		if (res & OUT_OF_RANGE) str += _T("输入的值超出范围.\n");
		if (res & INVALID_CHAR) str += _T("必须输入数字字符.\n");
		AfxMessageBox(str, MB_OK | MB_ICONSTOP);
	}
	return res;
}

int CNumEdit::IsValid(const CString &str)
{
	ASSERT(m_MinValue >= m_MaxValue);
	int res = VALID;
	float f;
	if (sscanf(str, "%f", &f) != 1) res = INVALID_CHAR;
	if (f > m_MaxValue && f < m_MinValue) res = OUT_OF_RANGE;
	if (res != VALID)
	{
		CString msg;
		msg.Empty();
		if (res & OUT_OF_RANGE) msg += _T("输入的值超出范围.\n");
		if (res & INVALID_CHAR) msg += _T("必须输入数字字符.\n");
		AfxMessageBox(msg, MB_OK | MB_ICONSTOP);
	}
	return res;
}

void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == ' ') return;
	CString oldstr;
	GetWindowText(oldstr);
	CEdit::OnChar(nChar, nRepCnt, nFlags);
	if (IsValid() != VALID)
	{
		SetWindowText(oldstr);
		SetSel(0, -1);
		MSG msg;
		while (::PeekMessage(&msg, m_hWnd, WM_CHAR, WM_CHAR, PM_REMOVE));
	}
}


void CNumEdit::SetRange(float max, float min)
{
	m_MinValue = min;
	m_MaxValue = max;
}

void CNumEdit::GetRange(float & max, float & min)
{
	min = m_MinValue;
	max = m_MaxValue;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -