📄 amsedit.h
字号:
#if !defined(AFX_AMS_EDIT_H__AC5ACB94_4363_11D3_9123_00105A6E5DE4__INCLUDED_)
#define AFX_AMS_EDIT_H__AC5ACB94_4363_11D3_9123_00105A6E5DE4__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// Edit.h : header file
// Created by: Alvaro Mendez - 07/17/2000
//
#include <afxwin.h>
/////////////////////////////////////////////////////////////////////////////
// CAMSEdit window
// Class CAMSEdit is the base class for all the other AMS CEdit classes.
// It provides some base functionality to set and get the text and change
// its text and background color.
//
class CAMSEdit : public CEdit
{
public:
// Construction/destruction
CAMSEdit();
virtual ~CAMSEdit();
// Operations
void SetText(const CString& strText);
CString GetText() const;
void SetBackgroundColor(COLORREF rgb);
COLORREF GetBackgroundColor() const;
void SetTextColor(COLORREF rgb);
COLORREF GetTextColor() const;
void DrawEdge(void);
protected:
virtual void Redraw();
virtual CString GetValidText() const;
virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult);
virtual bool ShouldEnter(TCHAR c) const;
protected:
CBrush m_brushBackground;
COLORREF m_rgbText;
bool m_bIsFocused ;
bool m_bIsMouseOver ;
private:
enum InternalFlags
{
None = 0x0000,
TextColorHasBeenSet = 0x0001
};
UINT m_uInternalFlags;
public:
// Class SelectionSaver is used to save an edit box's current
// selection and then restore it on destruction.
class SelectionSaver
{
public:
SelectionSaver(CEdit* pEdit);
SelectionSaver(CEdit* pEdit, int nStart, int nEnd);
~SelectionSaver();
void MoveTo(int nStart, int nEnd);
void MoveBy(int nStart, int nEnd);
void MoveBy(int nPos);
void operator+=(int nPos);
int GetStart() const;
int GetEnd() const;
protected:
CEdit* m_pEdit;
int m_nStart, m_nEnd;
};
// Class Behavior is an abstract base class used to define how an edit
// box will behave when it is used.
class Behavior
{
protected:
Behavior(CAMSEdit* pEdit);
public:
virtual CString _GetValidText() const = 0;
virtual void _OnChar(UINT uChar, UINT nRepCnt, UINT nFlags) = 0;
virtual void _OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual LONG _OnPaste(UINT wParam, LONG lParam);
protected:
virtual LRESULT _Default();
void _Redraw();
bool _ShouldEnter(TCHAR c) const;
protected:
CAMSEdit* m_pEdit;
};
friend class Behavior;
// The AlphanumericBehavior class is used to allow entry of alphanumeric
// characters. It can be restricted in terms of what characters cannot
// be inputed as well as how many are allowed altogether.
class AlphanumericBehavior : public Behavior
{
public:
AlphanumericBehavior(CAMSEdit* pEdit, int nMaxChars = 0, const CString& strInvalidChars = _T("%'*\"+?><:\\"));
// Operations
void SetInvalidCharacters(const CString& strInvalidChars);
const CString& GetInvalidCharacters() const;
void SetMaxCharacters(int nMaxChars);
int GetMaxCharacters() const;
protected:
virtual CString _GetValidText() const;
virtual void _OnChar(UINT uChar, UINT nRepCnt, UINT nFlags);
protected:
int m_nMaxChars;
CString m_strInvalidChars;
};
// The MaskedBehavior class is used to allow entry of numeric characters
// based on a given mask containing '#' characters to hold digits.
class MaskedBehavior : public Behavior
{
public:
// Construction
MaskedBehavior(CAMSEdit* pEdit, const CString& strMask = _T(""));
int MovePrevious(TCHAR c, int nPos); //前移一个有效位
int MoveNext(TCHAR c,int nPos); //后移一个有效位
BOOL IsEnd(int nPos, CString strText); //
public:
// Operations
void SetMask(const CString& strMask);
const CString& GetMask() const;
CString GetNumericText() const;
protected:
//0命令1正确100无发现-1错误
int CheckCode(TCHAR nChar, TCHAR cMask) const;
virtual CString _GetValidText() const;
virtual void _OnChar(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual void _OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags);
protected:
// Attributes
CString m_strMask;
};
// The NumericBehavior class is used to allow the entry of an actual numeric
// value into the edit control. It may be restricted by the number of digits
// before or after the decimal point (if any). If can also be set to use
// commas to separate and group thousands.
class NumericBehavior : public Behavior
{
public:
// Construction
NumericBehavior(CAMSEdit* pEdit, int nMaxWholeDigits = 9, int nMaxDecimalPlaces = 4);
public:
// Operations
void SetMaxWholeDigits(int nMaxWholeDigits);
int GetMaxWholeDigits() const;
void SetMaxDecimalPlaces(int nMaxDecimalPlaces);
int GetMaxDecimalPlaces() const;
void AllowNegative(bool bAllowNegative = true);
bool IsNegativeAllowed() const;
void SetDigitsInGroup(int nDigitsInGroup);
int GetDigitsInGroup() const;
void SetPrefix(const CString& strPrefix);
const CString& GetPrefix() const;
void SetMask(const CString& strMask);
CString GetMask() const;
double GetTextAsDouble() const;
protected:
virtual CString _GetValidText() const;
virtual void _OnChar(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual void _OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags);
int GetGroupSeparatorCount(const CString& strText) const;
CString GetNumericText(const CString& strText) const;
CString GetSeparatedText(const CString& strText) const;
void AdjustSeparators(int nCurrentSeparatorCount);
protected:
// Attributes
int m_nMaxWholeDigits; //整数位长
int m_nMaxDecimalPlaces; //小数位长
bool m_bAllowNegative; //是否使能无效值显示
TCHAR m_cNegativeSign; //无效值
TCHAR m_cDecimalPoint; //小数符号
TCHAR m_cGroupSeparator; //分隔符号
int m_nDigitsInGroup; //分隔符号位长
CString m_strPrefix; //预设首位串
};
//使用说明
//例:$123,456,789.12 正常值
// - 无值或无效值
//m_nMaxWholeDigits = 9
//m_nMaxDecimalPlaces = 2
//m_bAllowNegative = true
//m_cNegativeSign = -
//m_cDecimalPoint = .
//m_cGroupSeparator = ,
//m_nDigitsInGroup = 3
//m_strPrefix = $
// The DateBehavior class is used to allow the entry of date values.
class DateBehavior : public Behavior
{
public:
// Construction
DateBehavior(CAMSEdit* pEdit);
public:
// Operations
void SetDate(int nYear, int nMonth, int nDay);
void SetDate(const CTime& date);
void SetDate(const COleDateTime& date);
void SetDateToToday();
CTime GetDate() const;
COleDateTime GetOleDate() const;
int GetYear() const;
int GetMonth() const;
int GetDay() const;
void SetYear(int nYear);
void SetMonth(int nMonth);
void SetDay(int nDay);
bool IsValid() const;
bool CheckIfValid(bool bShowErrorIfNotValid = true);
void SetRange(const CTime& dateMin, const CTime& dateMax);
void SetRange(const COleDateTime& dateMin, const COleDateTime& dateMax);
void GetRange(CTime* pDateMin, CTime* pDateMax) const;
void GetRange(COleDateTime* pDateMin, COleDateTime* pDateMax) const;
void SetSeparator(TCHAR cSep);
TCHAR GetSeparator() const;
void ShowDayBeforeMonth(bool bDayBeforeMonth = true);
bool IsDayShownBeforeMonth() const;
enum Flags
{
None = 0x0000,
DayBeforeMonth = 0x1000
};
void ModifyFlags(UINT uAdd, UINT uRemove);
UINT GetFlags() const;
protected:
virtual CString _GetValidText() const;
virtual void _OnChar(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual void _OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual LONG _OnPaste(UINT wParam, LONG lParam);
protected:
// Helpers
bool AdjustMaxMonthAndDay();
bool AdjustMaxDay();
bool IsValid(const COleDateTime& date) const;
void ShowErrorMessage() const;
int GetValidMonth() const;
int GetMaxMonth() const;
int GetMinMonth() const;
int GetMonthStartPosition() const;
TCHAR GetMaxMonthDigit(int nPos) const;
TCHAR GetMinMonthDigit(int nPos) const;
bool IsValidMonthDigit(TCHAR c, int nPos) const;
bool IsValidMonth(int nMonth) const;
int GetValidDay() const;
int GetMaxDay() const;
int GetMinDay() const;
int GetDayStartPosition() const;
TCHAR GetMaxDayDigit(int nPos) const;
TCHAR GetMinDayDigit(int nPos) const;
bool IsValidDayDigit(TCHAR c, int nPos) const;
bool IsValidDay(int nDay) const;
int GetValidYear() const;
int GetYearStartPosition() const;
TCHAR GetMaxYearDigit(int nPos) const;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -