MyEdit.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "stdafx.h"
  2. #include "MyEdit.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
  9. CMyEdit::CMyEdit()
  10. {
  11. //默认是所有都可以输入
  12. SetInput(TYPE_BDFH | TYPE_NUM | TYPE_WORD | TYPE_WWORD);
  13. m_pOthers = NULL;
  14. }
  15. CMyEdit::CMyEdit(DWORD dwRight)
  16. : m_dwRight(dwRight)
  17. {
  18. m_pOthers = NULL;
  19. }
  20. CMyEdit::~CMyEdit()
  21. {
  22. if(m_pOthers)
  23. delete [] m_pOthers;
  24. }
  25. BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
  26. //{{AFX_MSG_MAP(CNumEdit)
  27. ON_WM_CHAR()
  28. //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////
  31. /*
  32. 函数:SetInput
  33. 描述:设置允许输入的权限
  34. 参数:
  35. DWORD dwRight 可输入权限
  36. 返回:
  37. */
  38. /////////////////////////////////////////////////////
  39. void CMyEdit::SetInput(const DWORD dwRight)
  40. {
  41. m_dwRight = dwRight;
  42. }
  43. /////////////////////////////////////////////////////
  44. /*
  45. 函数:SetOthers
  46. 描述:设置其它允许输入的字符
  47. 参数:
  48. const char* pOthers, 其它字符如:",./';[p[=jduf1234"
  49. const int nSize 串大小
  50. 返回:
  51. */
  52. /////////////////////////////////////////////////////
  53. void CMyEdit::SetOthers(const char* pOthers, const int nSize)
  54. {
  55. if(pOthers == NULL)
  56. return;
  57. if(m_pOthers)
  58. delete [] m_pOthers;
  59. m_pOthers = new char[nSize + 1];
  60. memset(m_pOthers, 0, nSize + 1);
  61. memcpy(m_pOthers, pOthers, nSize);
  62. }
  63. void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  64. {
  65. if (nChar == 8)
  66. CEdit::OnChar(nChar, nRepCnt, nFlags);
  67. //可输入标点符号
  68. if(m_dwRight & TYPE_BDFH)
  69. {
  70. if((nChar >= 32 && nChar <= 47) || (nChar >= 58 && nChar <= 64) || (nChar >= 91 && nChar <= 96) || (nChar >= 123 && nChar <= 126))
  71. {
  72. CEdit::OnChar(nChar, nRepCnt, nFlags);
  73. return;
  74. }
  75. }
  76. //可输入数字
  77. if(m_dwRight & TYPE_NUM)
  78. {
  79. if(nChar >= 48 && nChar <= 57)
  80. {
  81. CEdit::OnChar(nChar, nRepCnt, nFlags);
  82. return;
  83. }
  84. }
  85. //可输入字母
  86. if(m_dwRight & TYPE_WORD)
  87. {
  88. if((nChar >= 65 && nChar <= 90) || (nChar >= 97 && nChar <= 122) || (nChar < 0))
  89. {
  90. CEdit::OnChar(nChar, nRepCnt, nFlags);
  91. return;
  92. }
  93. }
  94. //可输入中文
  95. if(m_dwRight & TYPE_WWORD)
  96. {
  97. if(nChar < 0 || nChar > 127)
  98. {
  99. CEdit::OnChar(nChar, nRepCnt, nFlags);
  100. return;
  101. }
  102. }
  103. if(m_pOthers == NULL)
  104. return;
  105. //另外允许输入的字符
  106. for(int i=0; i<strlen(m_pOthers); i++)
  107. {
  108. if((int)(*(m_pOthers + i)) == nChar)
  109. {
  110. CEdit::OnChar(nChar, nRepCnt, nFlags);
  111. return;
  112. }
  113. }
  114. }