📄 amsedit.cpp
字号:
// amsEdit.cpp : implementation file for CEdit-derived classes
// Created by: Alvaro Mendez - 07/17/2000
//
#include "stdafx.h"
#include "amsEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <winuser.H>
#pragma warning (disable:4355) // disables: 'this': used in base member initializer list
/////////////////////////////////////////////////////////////////////////////
// CAMSEdit
CAMSEdit::CAMSEdit() :
m_rgbText(0),
m_uInternalFlags(None)
{
m_bIsFocused = FALSE;
m_bIsMouseOver = FALSE;
}
// Destruction (virtual)
CAMSEdit::~CAMSEdit()
{
}
BEGIN_MESSAGE_MAP(CAMSEdit, CEdit)
//{{AFX_MSG_MAP(CAMSEdit)
ON_WM_NCPAINT()
ON_WM_GETDLGCODE()
ON_WM_KEYDOWN()
ON_WM_MOUSEMOVE()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CUT, OnCut)
ON_MESSAGE(WM_PASTE, OnPaste)
ON_MESSAGE(WM_CLEAR, OnClear)
ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()
void CAMSEdit::OnNcPaint()
{
// TODO: Add your message handler code here
Default();
DrawEdge();
// Do not call CEdit::OnNcPaint() for painting messages
}
// Returns the current window's text.
CString CAMSEdit::GetText() const
{
CString strWindowText;
GetWindowText(strWindowText);
return strWindowText;
}
// Sets the window's text to strText.
void CAMSEdit::SetText(const CString& strText)
{
SetWindowText(strText);
Redraw();
}
// Sets the background color to the given rgb.
void CAMSEdit::SetBackgroundColor(COLORREF rgb)
{
m_brushBackground.DeleteObject();
m_brushBackground.CreateSolidBrush(rgb);
Invalidate();
}
// Returns the RGB for the background color.
COLORREF CAMSEdit::GetBackgroundColor() const
{
CAMSEdit* pThis = (CAMSEdit*)this;
if (!m_brushBackground.GetSafeHandle())
{
COLORREF rgb = pThis->GetDC()->GetBkColor();
pThis->m_brushBackground.CreateSolidBrush(rgb);
return rgb;
}
LOGBRUSH lb;
pThis->m_brushBackground.GetLogBrush(&lb);
return lb.lbColor;
}
// Sets the text color to the given rgb.
void CAMSEdit::SetTextColor(COLORREF rgb)
{
m_rgbText = rgb;
m_uInternalFlags |= TextColorHasBeenSet;
Invalidate();
}
// Returns the RGB for the text color.
COLORREF CAMSEdit::GetTextColor() const
{
if (!(m_uInternalFlags & TextColorHasBeenSet))
{
CAMSEdit* pThis = (CAMSEdit*)this;
pThis->m_rgbText = pThis->GetDC()->GetTextColor();
pThis->m_uInternalFlags |= TextColorHasBeenSet;
}
return m_rgbText;
}
// Returns the current window's text in a valid format.
CString CAMSEdit::GetValidText() const
{
return GetText();
}
// Redraws the window's text.
void CAMSEdit::Redraw()
{
if (!::IsWindow(m_hWnd))
return;
CString strText = GetValidText();
if (strText != GetText())
SetWindowText(strText);
}
// Returns true if the given text should be entered.
bool CAMSEdit::ShouldEnter(TCHAR c) const
{
return true;
}
// Cuts the current selection into the clipboard.
LONG CAMSEdit::OnCut(UINT, LONG)
{
int nStart, nEnd;
GetSel(nStart, nEnd);
CString str;
if (nStart < nEnd)
{
SendMessage(WM_COPY); // copy the selection and...
// SendMessage(WM_KEYDOWN, VK_DELETE); // delete it
GetWindowText(str);
str = str.Left(nStart);
SetWindowText(str);
SetSel(nStart, nStart);
}
return 0;
}
// Clears the current selection.
LONG CAMSEdit::OnClear(UINT wParam, LONG lParam)
{
int nStart, nEnd;
GetSel(nStart, nEnd);
if (nStart < nEnd)
SendMessage(WM_KEYDOWN, VK_DELETE); // delete the selection
return 0;
}
// Pastes the text from the clipboard onto the current selection.
LONG CAMSEdit::OnPaste(UINT, LONG)
{
int nStart, nEnd;
GetSel(nStart, nEnd);
CEdit::Default();
CString strText = GetValidText();
if (strText != GetText())
{
SetWindowText(strText);
SetSel(nStart, nEnd);
}
return 0;
}
// Handles drawing the text and background using the designated colors
BOOL CAMSEdit::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
{
if ((message == WM_CTLCOLOREDIT || message == WM_CTLCOLORSTATIC) && (m_brushBackground.GetSafeHandle() || m_uInternalFlags & TextColorHasBeenSet))
{
CDC* pDC = CDC::FromHandle((HDC)wParam);
if (m_rgbText)
pDC->SetTextColor(m_rgbText);
// Set the text background to the requested background color
pDC->SetBkColor(GetBackgroundColor());
*pLResult = (LRESULT)m_brushBackground.GetSafeHandle();
return TRUE;
}
return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
}
// Insures that text set via SetWindowText is valid.
LONG CAMSEdit::OnSetText(UINT wParam, LONG lParam)
{
LONG nResult = CEdit::Default();
CString strText = GetValidText();
if (strText != (LPCTSTR)lParam)
SetWindowText(strText);
return nResult;
}
/////////////////////////////////////////////////////////////////////////////
// CAMSEdit::SelectionSaver
// Constructs the selection saver object for the given edit control.
// It then saves the edit control's current selection.
CAMSEdit::SelectionSaver::SelectionSaver(CEdit* pEdit) :
m_pEdit(pEdit)
{
ASSERT(pEdit);
pEdit->GetSel(m_nStart, m_nEnd);
}
// Constructs the selection saver object for the given edit control.
// It then saves the given nStart and nEnd values.
CAMSEdit::SelectionSaver::SelectionSaver(CEdit* pEdit, int nStart, int nEnd) :
m_pEdit(pEdit),
m_nStart(nStart),
m_nEnd(nEnd)
{
ASSERT(pEdit);
ASSERT(nStart <= nEnd);
}
// Destroys the object and restores the selection to the saved start and end values.
CAMSEdit::SelectionSaver::~SelectionSaver()
{
m_pEdit->SetSel(m_nStart, m_nEnd, TRUE);
}
// Changes the start and end values to nStart and nEnd respectively.
void CAMSEdit::SelectionSaver::MoveTo(int nStart, int nEnd)
{
ASSERT(nStart <= nEnd);
m_nStart = nStart;
m_nEnd = nEnd;
}
// Changes the start and end values by nStart and nEnd respectively.
void CAMSEdit::SelectionSaver::MoveBy(int nStart, int nEnd)
{
m_nStart += nStart;
m_nEnd += nEnd;
ASSERT(m_nStart <= m_nEnd);
}
// Changes both the start and end values by nPos.
void CAMSEdit::SelectionSaver::MoveBy(int nPos)
{
m_nStart += nPos;
m_nEnd += nPos;
}
// Changes both the start and end values by nPos.
void CAMSEdit::SelectionSaver::operator+=(int nPos)
{
MoveBy(nPos);
}
// Returns the value for the selection's start.
int CAMSEdit::SelectionSaver::GetStart() const
{
return m_nStart;
}
// Returns the value for the selection's end.
int CAMSEdit::SelectionSaver::GetEnd() const
{
return m_nEnd;
}
/////////////////////////////////////////////////////////////////////////////
// CAMSEdit::Behavior
CAMSEdit::Behavior::Behavior(CAMSEdit* pEdit) :
m_pEdit(pEdit)
{
ASSERT(m_pEdit);
}
void CAMSEdit::Behavior::_OnChar(UINT uChar, UINT nRepCnt, UINT nFlags)
{
m_pEdit->OnChar(uChar, nRepCnt, nFlags);
}
void CAMSEdit::Behavior::_OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags)
{
m_pEdit->OnKeyDown(uChar, nRepCnt, nFlags);
}
LONG CAMSEdit::Behavior::_OnPaste(UINT wParam, LONG lParam)
{
return m_pEdit->OnPaste(wParam, lParam);
}
LRESULT CAMSEdit::Behavior::_Default()
{
return m_pEdit->Default();
}
void CAMSEdit::Behavior::_Redraw()
{
m_pEdit->Redraw();
}
bool CAMSEdit::Behavior::_ShouldEnter(TCHAR c) const
{
return m_pEdit->ShouldEnter(c);
}
/////////////////////////////////////////////////////////////////////////////
// CAMSEdit::AlphanumericBehavior
// Constructs the object using the given set of strInvalidChars
CAMSEdit::AlphanumericBehavior::AlphanumericBehavior(CAMSEdit* pEdit, int nMaxChars /*= 0*/, const CString& strInvalidChars /*= _T("%'*\"+?><:\\"")*/) :
Behavior(pEdit),
m_nMaxChars(nMaxChars),
m_strInvalidChars(strInvalidChars)
{
ASSERT(m_nMaxChars >= 0);
}
// Sets the characters to be considered invalid for text input.
// See also: GetInvalidCharacters
void CAMSEdit::AlphanumericBehavior::SetInvalidCharacters(const CString& strInvalidChars)
{
if (m_strInvalidChars == strInvalidChars)
return;
m_strInvalidChars = strInvalidChars;
_Redraw();
}
// Returns the characters considered invalid for text input.
// See also: SetInvalidCharacters
const CString& CAMSEdit::AlphanumericBehavior::GetInvalidCharacters() const
{
return m_strInvalidChars;
}
// Sets the maximum number of characters to allow for input.
void CAMSEdit::AlphanumericBehavior::SetMaxCharacters(int nMaxChars)
{
if (m_nMaxChars == nMaxChars)
return;
m_nMaxChars = nMaxChars;
_Redraw();
}
// Returns the characters considered invalid for text input.
// See also: SetInvalidCharacters
int CAMSEdit::AlphanumericBehavior::GetMaxCharacters() const
{
return m_nMaxChars;
}
// Returns the current window's text in a valid format
CString CAMSEdit::AlphanumericBehavior::_GetValidText() const
{
CString strText = m_pEdit->GetText();
CString strNewText = strText.Left(m_nMaxChars ? m_nMaxChars : strText.GetLength());
// Remove any invalid characters from the current window text
for (int iPos = strNewText.GetLength() - 1; iPos >= 0; iPos--)
{
if (m_strInvalidChars.Find(strNewText[iPos]) >= 0)
strNewText = strNewText.Left(iPos) + strNewText.Mid(iPos + 1);
}
return strNewText;
}
void CAMSEdit::AlphanumericBehavior::_OnChar(UINT uChar, UINT nRepCnt, UINT nFlags)
{
if (!m_strInvalidChars.IsEmpty())
{
// Check if the character is invalid
if (m_strInvalidChars.Find((TCHAR)uChar) >= 0)
{
MessageBeep(MB_ICONEXCLAMATION);
return;
}
}
TCHAR c = (TCHAR)uChar;
// If the number of characters is already at Max, overwrite
CString strText = m_pEdit->GetText();
if (strText.GetLength() == m_nMaxChars && m_nMaxChars && _istprint(c))
{
int nStart, nEnd;
m_pEdit->GetSel(nStart, nEnd);
if (nStart < m_nMaxChars && _ShouldEnter(c))
{
m_pEdit->SetSel(nStart, nStart + 1);
m_pEdit->ReplaceSel(CString(c), TRUE);
}
return;
}
if (_ShouldEnter(c))
Behavior::_OnChar(uChar, nRepCnt, nFlags);
}
/////////////////////////////////////////////////////////////////////////////
// CAMSEdit::MaskedBehavior
CAMSEdit::MaskedBehavior::MaskedBehavior(CAMSEdit* pEdit, const CString& strMask /*= _T("")*/) :
Behavior(pEdit),
m_strMask(strMask)
{
}
const CString& CAMSEdit::MaskedBehavior::GetMask() const
{
return m_strMask;
}
void CAMSEdit::MaskedBehavior::SetMask(const CString& strMask)
{
if (m_strMask == strMask)
return;
m_strMask = strMask;
_Redraw();
}
CString CAMSEdit::MaskedBehavior::GetNumericText() const
{
CString strText = m_pEdit->GetText();
CString strResult;
for (int iPos = 0, nLen = strText.GetLength(); iPos < nLen; iPos++)
{
TCHAR c = strText[iPos];
if (_istdigit(c))
strResult += c;
}
return strResult;
}
int CAMSEdit::MaskedBehavior::MovePrevious(TCHAR c,int nPos) //前移一个有效位
{
for(int i = nPos-1; i >= 0 && i < m_strMask.GetLength(); i--)
{
TCHAR cMask = m_strMask.GetAt(i);
if (CheckCode(c, cMask) == 1) return i;
}
return -1;
}
BOOL CAMSEdit::MaskedBehavior::IsEnd(int nPos, CString strText) //
{
int iRet = 0;
for(int i = 0 ; i < strText.GetLength() && i < m_strMask.GetLength(); i++)
{
TCHAR c = strText.GetAt(i);
TCHAR cMask = m_strMask.GetAt(i);
if (CheckCode(c, cMask) == 1) iRet = i;
}
if (iRet != nPos-1) return false;
return true;
}
int CAMSEdit::MaskedBehavior::MoveNext(TCHAR c, int nPos) //后移一个有效位
{
int iRet;
for(int i = nPos; i < m_strMask.GetLength(); i++)
{
TCHAR cMask = m_strMask.GetAt(i);
iRet = CheckCode(c, cMask);
if (iRet == 1) return i;
else if(iRet == -1) return -1;
}
return -1;
}
//0命令1正确100无发现-1错误
int CAMSEdit::MaskedBehavior::CheckCode(TCHAR nChar,TCHAR cMask) const
{
switch(cMask)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -