123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include "stdafx.h"
- #include "MyEdit.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
- CMyEdit::CMyEdit()
- {
- //默认是所有都可以输入
- SetInput(TYPE_BDFH | TYPE_NUM | TYPE_WORD | TYPE_WWORD);
- m_pOthers = NULL;
- }
- CMyEdit::CMyEdit(DWORD dwRight)
- : m_dwRight(dwRight)
- {
- m_pOthers = NULL;
- }
- CMyEdit::~CMyEdit()
- {
- if(m_pOthers)
- delete [] m_pOthers;
- }
- BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
- //{{AFX_MSG_MAP(CNumEdit)
- ON_WM_CHAR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////
- /*
- 函数:SetInput
- 描述:设置允许输入的权限
- 参数:
- DWORD dwRight 可输入权限
- 返回:
- */
- /////////////////////////////////////////////////////
- void CMyEdit::SetInput(const DWORD dwRight)
- {
- m_dwRight = dwRight;
- }
- /////////////////////////////////////////////////////
- /*
- 函数:SetOthers
- 描述:设置其它允许输入的字符
- 参数:
- const char* pOthers, 其它字符如:",./';[p[=jduf1234"
- const int nSize 串大小
- 返回:
- */
- /////////////////////////////////////////////////////
- void CMyEdit::SetOthers(const char* pOthers, const int nSize)
- {
- if(pOthers == NULL)
- return;
- if(m_pOthers)
- delete [] m_pOthers;
- m_pOthers = new char[nSize + 1];
- memset(m_pOthers, 0, nSize + 1);
- memcpy(m_pOthers, pOthers, nSize);
- }
- void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if (nChar == 8)
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- //可输入标点符号
- if(m_dwRight & TYPE_BDFH)
- {
- if((nChar >= 32 && nChar <= 47) || (nChar >= 58 && nChar <= 64) || (nChar >= 91 && nChar <= 96) || (nChar >= 123 && nChar <= 126))
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- return;
- }
- }
- //可输入数字
- if(m_dwRight & TYPE_NUM)
- {
- if(nChar >= 48 && nChar <= 57)
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- return;
- }
- }
- //可输入字母
- if(m_dwRight & TYPE_WORD)
- {
- if((nChar >= 65 && nChar <= 90) || (nChar >= 97 && nChar <= 122) || (nChar < 0))
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- return;
- }
- }
- //可输入中文
- if(m_dwRight & TYPE_WWORD)
- {
- if(nChar < 0 || nChar > 127)
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- return;
- }
- }
- if(m_pOthers == NULL)
- return;
- //另外允许输入的字符
- for(int i=0; i<strlen(m_pOthers); i++)
- {
- if((int)(*(m_pOthers + i)) == nChar)
- {
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- return;
- }
- }
- }
|