📄 numericedit.cpp
字号:
// NumericEdit.cpp : implementation file
//
#include "stdafx.h"
#include "NumericEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////
#define Epsl 0.000001
//小于: -1
//属于: 0
//大于: 1
int CalcOutRange(float fMinVal, float fMaxVal, float Val)
{
if ((Val - fMaxVal) > Epsl) //超出最大值
return 1;
if ((fMinVal - Val) > Epsl) //超出最小值
return -1;
if ( ((int)(1000*fMinVal) <= (int)(1000*Val)) && ((int)(1000*fMaxVal) >= (int)(1000*Val)) ) //属于
return 0;
return 0;
}
int CalcOutRange(int nMinVal, int nMaxVal, int Val)
{
if (Val < nMinVal) //超出最小值
return -1;
if (nMinVal <= Val && nMaxVal >= Val) //属于
return 0;
if (Val > nMaxVal) //超出最大值
return 1;
return 0;
}
//////////////////////////////////////////////////////////////////////////
//
void CNumericEdit::CheckRange()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int nSelfVal = 0;
float fSelfVal = 0;
CString rString;
this->GetWindowText(rString);
if (m_sInnerObj.sType == 1) /*整形*/
nSelfVal = ::atol(rString);
else
fSelfVal = ::atof(rString); /*浮点*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//设定合理量程
char szBuff[32];
if (m_sInnerObj.sType == 1) /*整形*/
{
if (::CalcOutRange(m_sInnerObj.m_RngVal_Min.nIVal, m_sInnerObj.m_RngVal_Max.nIVal, nSelfVal) == 1)
{
::sprintf(szBuff, "%d", m_sInnerObj.m_RngVal_Max.nIVal);
this->SetWindowText((LPCTSTR)szBuff);
}
if (::CalcOutRange(m_sInnerObj.m_RngVal_Min.nIVal, m_sInnerObj.m_RngVal_Max.nIVal, nSelfVal) == -1)
{
::sprintf(szBuff, "%d", m_sInnerObj.m_RngVal_Min.nIVal);
this->SetWindowText((LPCTSTR)szBuff);
}
}
else /*浮点*/
{
if (::CalcOutRange(m_sInnerObj.m_RngVal_Min.nFVal, m_sInnerObj.m_RngVal_Max.nFVal, fSelfVal) == 1)
{
::sprintf(szBuff, "%.2f", m_sInnerObj.m_RngVal_Max.nFVal);
this->SetWindowText((LPCTSTR)szBuff);
}
if (::CalcOutRange(m_sInnerObj.m_RngVal_Min.nFVal, m_sInnerObj.m_RngVal_Max.nFVal, fSelfVal) == -1)
{
::sprintf(szBuff, "%.2f", m_sInnerObj.m_RngVal_Min.nFVal);
this->SetWindowText((LPCTSTR)szBuff);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//报警
CWnd* pWndTarget = GetParent();
if (pWndTarget != NULL)
{
if (m_sInnerObj.sType == 1) /*整形*/
{
if (::CalcOutRange(m_sInnerObj.m_RngVal_Min.nIVal, m_sInnerObj.m_RngVal_Max.nIVal, nSelfVal) != 0)
pWndTarget->SendMessage(WM_USER+0x299/*超出范围*/, this->GetDlgCtrlID(), 0);
}
else /*浮点*/
{
if (::CalcOutRange(m_sInnerObj.m_RngVal_Min.nFVal, m_sInnerObj.m_RngVal_Max.nFVal, fSelfVal) != 0)
pWndTarget->SendMessage(WM_USER+0x299/*超出范围*/, this->GetDlgCtrlID(), 0);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CNumericEdit
CNumericEdit::CNumericEdit()
:m_nDotNum(0),
m_bEnable(TRUE),
m_nNegTag(0)
{
//SetRange(0, 1200);
}
CNumericEdit::~CNumericEdit()
{
}
BEGIN_MESSAGE_MAP(CNumericEdit, CEdit)
//{{AFX_MSG_MAP(CNumericEdit)
ON_WM_CHAR()
ON_WM_KEYDOWN()
ON_WM_DRAWITEM()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNumericEdit message handlers
void CNumericEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!m_bEnable)
return ;
//删除一个字符
if (nChar == 8)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
return ;
}
if (nChar != 46)
if ((nChar < '0') || (nChar > '9'))
return ;
//此时拒绝输入!
if (m_nDotNum ==1 && nChar == 46)
return ;
CString rString;
this->GetWindowText(rString);
m_nDotNum = !(rString.FindOneOf(".")<0); //不存在,则为1!
CEdit::OnChar(nChar, nRepCnt, nFlags);
//
this->CheckRange();
}
void CNumericEdit::SetRange(float fMinVal, float fMaxVal)
{
m_sInnerObj.sType = 0;
//
m_sInnerObj.m_RngVal_Min.nFVal= fMinVal; //最小值
m_sInnerObj.m_RngVal_Max.nFVal= fMaxVal; //最大值
}
void CNumericEdit::SetRange(int nMinVal, int nMaxVal)
{
m_sInnerObj.sType = 1;
//
m_sInnerObj.m_RngVal_Min.nIVal= nMinVal; //最小值
m_sInnerObj.m_RngVal_Max.nIVal= nMaxVal; //最大值
}
void CNumericEdit::SetEnbl(BOOL bEnable)
{
m_bEnable = bEnable;
}
void CNumericEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CNumericEdit::PreTranslateMessage(MSG* pMsg)
{
//if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST)
//{
// if(pMsg->wParam==VK_RETURN )
// {
//HWND hWnd=::GetFocus();
//int iID=::GetDlgCtrlID(hWnd);
// }
//}
return CEdit::PreTranslateMessage(pMsg);
}
void CNumericEdit::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
// CDC sDc = CDC(lpDrawItemStruct->hDC);
// COLORREF oldColor = sDc.SetTextColor(RGB(127, 64, 0));
//
// sDc.DeleteDC();
CEdit::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
void CNumericEdit::OnSetFocus(CWnd* pOldWnd)
{
CEdit::OnSetFocus(pOldWnd);
//
CWnd* pWndTarget = GetParent();
if (pWndTarget != NULL)
{
pWndTarget->SendMessage(WM_USER+0x290, this->GetDlgCtrlID(), 0);
}
}
void CNumericEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
this->CheckRange();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -