NumEdit.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // NumEdit.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "NumEdit.h"
  5. // CNumEdit
  6. IMPLEMENT_DYNAMIC(CNumEdit, CEdit)
  7. CNumEdit::CNumEdit()
  8. {
  9. m_bNegative = FALSE;
  10. m_bPoint = FALSE;
  11. m_bZero = FALSE;
  12. }
  13. CNumEdit::~CNumEdit()
  14. {
  15. }
  16. BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
  17. ON_WM_CHAR()
  18. END_MESSAGE_MAP()
  19. // CNumEdit 消息处理程序
  20. void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  21. {
  22. // 获取插入符在客户区的坐标值;
  23. POINT caret;
  24. ::GetCaretPos(&caret);
  25. // 根据插入符位置获取最近的字符索引(插入符的前一个字符位置);
  26. caret.x = LOWORD(CharFromPos(caret));
  27. CString strEdit;
  28. GetWindowText(strEdit);
  29. // 退格键处理;
  30. if (nChar == VK_BACK && strEdit.GetLength()) //退格键;
  31. {
  32. // 是否删除了小数点;
  33. if ( caret.x && strEdit[caret.x-1] == _T('.') )
  34. m_bPoint = FALSE;
  35. // 是否删除了负号;
  36. if ( caret.x && strEdit[caret.x-1] == _T('-') )
  37. m_bNegative = FALSE;
  38. if ( m_bNegative )
  39. {
  40. if ( m_bPoint )
  41. {
  42. if ( m_bZero && caret.x == 2 )
  43. {
  44. m_bZero = FALSE;
  45. }
  46. }
  47. else
  48. {
  49. if ( m_bZero && caret.x == 2 && ( strEdit.GetLength() < 2 || strEdit[2] != _T('0')) )
  50. {
  51. m_bZero = FALSE;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. if ( m_bPoint )
  58. {
  59. if ( m_bZero && caret.x == 1 )
  60. {
  61. m_bZero = FALSE;
  62. }
  63. }
  64. else
  65. {
  66. if ( m_bZero && caret.x == 1 && ( strEdit.GetLength() < 1 || strEdit[1] != _T('0')) )
  67. {
  68. m_bZero = FALSE;
  69. }
  70. }
  71. }
  72. CEdit::OnChar(nChar, nRepCnt, nFlags);
  73. }
  74. // 数字键处理;
  75. if (isdigit(nChar))
  76. {
  77. // 判断是否已有负号,若字符在负号前,退出;
  78. if ( m_bNegative && caret.x == 0 )
  79. return;
  80. if ( m_bNegative )
  81. {
  82. // 负号前不能有任何字符,或者再输入负号;
  83. if ( caret.x == 0 || nChar == _T('-'))
  84. return;
  85. if ( m_bZero )
  86. {
  87. // 前置零前后都不能再有零;
  88. if ( (caret.x == 1 || caret.x == 2 ) && nChar == _T('0') )
  89. return;
  90. }
  91. else
  92. {
  93. // 负号的前置0只能在负号后面;
  94. if (caret.x == 1 && nChar == _T('0'))
  95. {
  96. m_bZero = TRUE;
  97. }
  98. }
  99. }
  100. else
  101. {
  102. if ( m_bZero )
  103. {
  104. // 已有前置0,不能再设置前置0;
  105. if ( (caret.x == 0 || caret.x == 1) && nChar == _T('0'))
  106. return;
  107. }
  108. else
  109. {
  110. if (caret.x == 0 && nChar == _T('0'))
  111. {
  112. m_bZero = TRUE;
  113. }
  114. }
  115. }
  116. CEdit::OnChar(nChar, nRepCnt, nFlags);
  117. }
  118. else if (nChar == '-' && !m_bNegative)
  119. {// 负号只能在第一位;
  120. if (0 != caret.x)
  121. return;
  122. m_bNegative = TRUE;
  123. CEdit::OnChar(nChar, nRepCnt, nFlags);
  124. }
  125. else if (nChar == '.' && !m_bPoint)
  126. {//小数点可以在第一位,但不能在负号后面;
  127. if ( m_bNegative && caret.x == 1)
  128. {// 小数点不能在负号后面,即第二个字符索引;
  129. return;
  130. }
  131. m_bPoint = TRUE;
  132. CEdit::OnChar(nChar, nRepCnt, nFlags);
  133. }
  134. }
  135. BOOL CNumEdit::PreTranslateMessage(MSG* pMsg)
  136. {
  137. // TODO: 在此添加专用代码和/或调用基类
  138. if ( pMsg->message == WM_LBUTTONUP )
  139. {
  140. CString strEdit;
  141. GetWindowText(strEdit);
  142. int nStart, nEnd;
  143. GetSel(nStart, nEnd);
  144. if ( nEnd > nStart) // 不区选字符;
  145. {
  146. printf("有区选光标\n");
  147. SetSel(-1,strEdit.GetLength());
  148. }
  149. }
  150. else if ( pMsg->message == WM_KEYDOWN )
  151. {
  152. if ( pMsg->wParam == VK_DELETE )
  153. {
  154. POINT caret;
  155. ::GetCaretPos(&caret);
  156. // 根据插入符位置获取最近的字符索引(插入符的前一个字符位置);
  157. LONG nPos = LOWORD(CharFromPos(caret));
  158. printf("NumEdit,%d,%d\t 插入符最近的字符位置:%d\n", caret.x, caret.y, nPos);
  159. CString strEdit;
  160. GetWindowText(strEdit);
  161. if (strEdit.GetLength())
  162. {
  163. // 是否删除了小数点或负号;
  164. if ( strEdit.GetAt(nPos) == _T('.') )
  165. m_bPoint = FALSE;
  166. if ( strEdit.GetAt(nPos) == _T('-') )
  167. m_bNegative = FALSE;
  168. if ( m_bNegative )
  169. {
  170. if ( m_bPoint )
  171. {
  172. if ( m_bZero && caret.x == 2 )
  173. {
  174. m_bZero = FALSE;
  175. }
  176. }
  177. else
  178. {
  179. if ( m_bZero && caret.x == 2 && ( strEdit.GetLength() < 2 || strEdit[2] != _T('0')) )
  180. {
  181. m_bZero = FALSE;
  182. }
  183. }
  184. }
  185. else
  186. {
  187. if ( m_bPoint )
  188. {
  189. if ( m_bZero && caret.x == 1 )
  190. {
  191. m_bZero = FALSE;
  192. }
  193. }
  194. else
  195. {
  196. if ( m_bZero && caret.x == 1 && ( strEdit.GetLength() < 1 || strEdit[1] != _T('0')) )
  197. {
  198. m_bZero = FALSE;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. return CEdit::PreTranslateMessage(pMsg);
  206. }