📄 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
// Modified by: Dominik Reichl - 26/02/2005
//
// #include <afxwin.h>
// #include <afxtempl.h>
// To export this code from an MFC Extension DLL uncomment the following line and then define _AMSEDIT_EXPORT inside the DLL's Project Settings.
// #define AMSEDIT_IN_DLL
// Import/Export macro for the classes below
#if defined(AMSEDIT_IN_DLL)
#if defined(_AMSEDIT_EXPORT)
#define AMSEDIT_EXPORT _declspec(dllexport)
#else
#define AMSEDIT_EXPORT _declspec(dllimport)
#endif
#else
#define AMSEDIT_EXPORT
#endif
// The following IDs are assigned for each class below to allow us to set which ones we need compiled
#define AMSEDIT_ALPHANUMERIC_CLASS 0x01
#define AMSEDIT_MASKED_CLASS 0x02
#define AMSEDIT_NUMERIC_CLASS 0x04
#define AMSEDIT_INTEGER_CLASS (AMSEDIT_NUMERIC_CLASS | 0x08)
#define AMSEDIT_CURRENCY_CLASS (AMSEDIT_NUMERIC_CLASS | 0x10)
#define AMSEDIT_DATE_CLASS 0x20
#define AMSEDIT_TIME_CLASS 0x40
#define AMSEDIT_DATETIME_CLASS (AMSEDIT_DATE_CLASS | AMSEDIT_TIME_CLASS)
#define AMSEDIT_ALL_CLASSES (AMSEDIT_ALPHANUMERIC_CLASS | AMSEDIT_MASKED_CLASS | AMSEDIT_INTEGER_CLASS | AMSEDIT_CURRENCY_CLASS | AMSEDIT_DATETIME_CLASS)
// If your program does not need all the CAMSEdit classes below, you can reduce
// the size of your executable by selecting just the classes you want to be compiled
// via the following macro. Use the IDs defined above and "OR" together the classes you need.
// #define AMSEDIT_COMPILED_CLASSES AMSEDIT_ALL_CLASSES
#define AMSEDIT_COMPILED_CLASSES AMSEDIT_DATETIME_CLASS
/////////////////////////////////////////////////////////////////////////////
// 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 AMSEDIT_EXPORT CAMSEdit : public CEdit
{
public:
// Construction/destruction
CAMSEdit();
virtual ~CAMSEdit();
// Operations
void SetText(const CString& strText);
CString GetText() const;
CString GetTrimmedText() const;
void SetBackgroundColor(COLORREF rgb);
COLORREF GetBackgroundColor() const;
void SetTextColor(COLORREF rgb);
COLORREF GetTextColor() const;
bool IsReadOnly() const;
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;
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 AMSEDIT_EXPORT 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;
void Update();
void Disable();
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. Note that its virtual member functions start
// with an underscore; this avoids naming conflicts when multiply inheriting.
class AMSEDIT_EXPORT Behavior
{
protected:
Behavior(CAMSEdit* pEdit);
virtual ~Behavior();
public:
bool ModifyFlags(UINT uAdd, UINT uRemove);
UINT GetFlags() const;
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 void _OnKillFocus(CWnd* pNewWnd);
virtual LRESULT _OnPaste(WPARAM wParam, LPARAM lParam);
protected:
// Wrappers to allow access to protected members of CAMSEdit
virtual LRESULT _Default();
virtual void _Redraw();
virtual bool _ShouldEnter(TCHAR c) const;
protected:
CAMSEdit* m_pEdit;
UINT m_uFlags;
};
friend class Behavior;
#if (AMSEDIT_COMPILED_CLASSES & AMSEDIT_ALPHANUMERIC_CLASS)
// 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 AMSEDIT_EXPORT 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;
};
#endif // (AMSEDIT_COMPILED_CLASSES & AMSEDIT_ALPHANUMERIC_CLASS)
#if (AMSEDIT_COMPILED_CLASSES & AMSEDIT_MASKED_CLASS)
// The MaskedBehavior class is used to allow entry of numeric characters
// based on a given mask containing '#' characters to hold digits.
class AMSEDIT_EXPORT MaskedBehavior : public Behavior
{
public:
// Construction
MaskedBehavior(CAMSEdit* pEdit, const CString& strMask = _T(""));
public:
// Operations
void SetMask(const CString& strMask);
const CString& GetMask() const;
CString GetNumericText() const;
// The Symbol class represents a character which may be added to the mask and then interpreted by the
// MaskedBehavior class to validate the input from the user and possibly convert it to something else.
class AMSEDIT_EXPORT Symbol
{
public:
#ifndef _UNICODE
typedef int (*ValidationFunction)(UINT); // designed for functions such as _istdigit, _istalpha
typedef UINT (*ConversionFunction)(UINT); // designed for functions such as _totupper, _totlower
#else
typedef int (*ValidationFunction)(WCHAR);
typedef WCHAR (*ConversionFunction)(WCHAR);
#endif
Symbol();
Symbol(TCHAR cSymbol, ValidationFunction fnValidation, ConversionFunction fnConversion = NULL);
virtual ~Symbol();
virtual bool Validate(TCHAR c) const;
virtual TCHAR Convert(TCHAR c) const;
void Set(TCHAR cSymbol);
TCHAR Get() const;
operator TCHAR() const;
protected:
TCHAR m_cSymbol;
ValidationFunction m_fnValidation;
ConversionFunction m_fnConversion;
};
typedef CArray<Symbol, Symbol const&> SymbolArray;
SymbolArray& GetSymbolArray();
protected:
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;
SymbolArray m_arraySymbols;
};
#endif // (AMSEDIT_COMPILED_CLASSES & AMSEDIT_MASKED_CLASS)
#if (AMSEDIT_COMPILED_CLASSES & AMSEDIT_NUMERIC_CLASS)
// 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 AMSEDIT_EXPORT NumericBehavior : public Behavior
{
public:
// Construction
NumericBehavior(CAMSEdit* pEdit, int nMaxWholeDigits = 9, int nMaxDecimalPlaces = 4);
public:
// Operations
void SetDouble(double dText, bool bTrimTrailingZeros = true);
double GetDouble() const;
void SetInt(int nText);
int GetInt() const;
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 SetSeparators(TCHAR cDecimal, TCHAR cGroup);
void GetSeparators(TCHAR* pcDecimal, TCHAR* pcGroup) const;
void SetPrefix(const CString& strPrefix);
const CString& GetPrefix() const;
void SetMask(const CString& strMask);
CString GetMask() const;
void SetRange(double dMin, double dMax);
void GetRange(double* pdMin, double* pdMax) const;
virtual bool IsValid() const;
bool CheckIfValid(bool bShowErrorIfNotValid = true);
enum Flags
{
None = 0x0000,
CannotBeNegative = 0x1000,
AddDecimalAfterMaxWholeDigits = 0x2000,
PadWithZerosAfterDecimalWhenTextChanges = 0x4000,
PadWithZerosAfterDecimalWhenTextIsSet = 0x8000,
OnKillFocus_Beep_IfInvalid = 0x0001,
OnKillFocus_Beep_IfEmpty = 0x0002,
OnKillFocus_Beep = 0x0003,
OnKillFocus_SetValid_IfInvalid = 0x0004,
OnKillFocus_SetValid_IfEmpty = 0x0008,
OnKillFocus_SetValid = 0x000C,
OnKillFocus_SetFocus_IfInvalid = 0x0010,
OnKillFocus_SetFocus_IfEmpty = 0x0020,
OnKillFocus_SetFocus = 0x0030,
OnKillFocus_ShowMessage_IfInvalid = 0x0050,
OnKillFocus_ShowMessage_IfEmpty = 0x00A0,
OnKillFocus_ShowMessage = 0x00F0,
OnKillFocus_PadWithZerosBeforeDecimal = 0x0100,
OnKillFocus_PadWithZerosAfterDecimal = 0x0200,
OnKillFocus_DontPadWithZerosIfEmpty = 0x0400,
OnKillFocus_RemoveExtraLeadingZeros = 0x0800,
OnKillFocus_Max = 0x0FFF
};
protected:
virtual CString _GetValidText() const;
virtual void _OnChar(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual void _OnKeyDown(UINT uChar, UINT nRepCnt, UINT nFlags);
virtual void _OnKillFocus(CWnd* pNewWnd);
int GetGroupSeparatorCount(const CString& strText) const;
CString GetNumericText(const CString& strText, bool bUseMathSymbols = false) const;
CString GetDoubleText(double dText, bool bTrimTrailingZeros = true) const;
CString GetSeparatedText(const CString& strText) const;
void AdjustSeparators(int nCurrentSeparatorCount);
static void InsertZeros(CString* pStrText, int nPos, int nCount);
virtual void ShowErrorMessage() const;
void AdjustWithinRange();
protected:
// Attributes
int m_nMaxWholeDigits;
int m_nMaxDecimalPlaces;
TCHAR m_cNegativeSign;
TCHAR m_cDecimalPoint;
TCHAR m_cGroupSeparator;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -