#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