MFC:整数编辑框CEdit

来源:互联网 发布:儿童画画软件 编辑:程序博客网 时间:2024/05/17 21:41

头文件(NumEdit.h)代码:

#pragma onceclass CNumEdit : public CEdit{DECLARE_DYNAMIC(CNumEdit)public:CNumEdit();virtual ~CNumEdit();protected:DECLARE_MESSAGE_MAP()public:afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);afx_msg HBRUSH CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/);};
源文件(NumEdit.cpp)代码:

#include "stdafx.h"#include "NumEdit.h"IMPLEMENT_DYNAMIC(CNumEdit, CEdit)CNumEdit::CNumEdit(){}CNumEdit::~CNumEdit(){}BEGIN_MESSAGE_MAP(CNumEdit, CEdit)ON_WM_CHAR()ON_WM_CTLCOLOR_REFLECT()END_MESSAGE_MAP()void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags){TCHAR ch = nChar;CString strText;if (ch == '-'){GetWindowText(strText);int len = strText.GetLength();if (len <= 0){CEdit::OnChar(nChar, nRepCnt, nFlags);}else {if (strText.Find('-') < 0){SetSel(0, 0, FALSE);CEdit::OnChar(nChar, nRepCnt, nFlags);}}}else if (ch >= '0' && ch <= '9'){GetWindowText(strText);if (strText.Find('0') == 0){//this->Clear();this->SetWindowText(_T(""));}if (strText.Find('-') == 0 && 1 == HIWORD(this->GetSel()) && ch == '0')//int nSel = HIWORD(this->GetSel());  获取光标位置{return;}CEdit::OnChar(nChar, nRepCnt, nFlags);}else if (ch == 0x08 || ch == 0x09){CEdit::OnChar(nChar, nRepCnt, nFlags);}return;}HBRUSH CNumEdit::CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/){CDC* dc = GetDC();CRect rect;GetClientRect(rect);rect.InflateRect(1, 1, 1, 1);CBrush brush(FrameRectColor);dc->FrameRect(rect, &brush);DeleteObject(brush);ReleaseDC(dc);return NULL;}
原创粉丝点击