NumEdit.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // NumEdit.cpp : implementation file
  2. //
  3. // By Mike Scanlon, 8/26/98
  4. #include "stdafx.h"
  5. #include "NumEdit.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CNumEdit
  13. CNumEdit::CNumEdit()
  14. {
  15. }
  16. CNumEdit::~CNumEdit()
  17. {
  18. }
  19. BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
  20. //{{AFX_MSG_MAP(CNumEdit)
  21. ON_WM_CHAR()
  22. //}}AFX_MSG_MAP
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CNumEdit message handlers
  26. void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  27. {
  28. // TODO: Add your message handler code here and/or call default
  29. if (nChar == 8)
  30. CEdit::OnChar(nChar, nRepCnt, nFlags);
  31. POINT caret;
  32. ::GetCaretPos (&caret);
  33. caret.x = LOWORD (CharFromPos (caret));
  34. CString text;
  35. GetWindowText (text);
  36. if (isdigit(nChar))
  37. CEdit::OnChar(nChar, nRepCnt, nFlags);
  38. else if (nChar == '-')
  39. {
  40. if (!caret.x)
  41. {
  42. if (((text.GetLength() > 0) && (text[0]!='-')) || (text.GetLength()==0))
  43. CEdit::OnChar(nChar, nRepCnt, nFlags);
  44. }
  45. else
  46. {
  47. if ((text.GetAt(caret.x-1) == 'e') || (text.GetAt(caret.x-1) == 'E'))
  48. CEdit::OnChar(nChar, nRepCnt, nFlags);
  49. }
  50. }
  51. /* else if ((nChar == 'e') || (nChar == 'E'))
  52. {
  53. if ((caret.x == 1) && (text[0] == '-'))
  54. return ;
  55. if (caret.x)
  56. {
  57. for (int i=0; i<text.GetLength(); i++)
  58. {
  59. if ((text[i] == 'e') ||(text[i] == 'E'))
  60. return ;
  61. }
  62. CEdit::OnChar(nChar, nRepCnt, nFlags);
  63. }
  64. }*/
  65. else if (nChar == '.')
  66. {
  67. int i = 0;
  68. for ( i=0; i<text.GetLength(); i++)
  69. {
  70. if (text[i] == '.')
  71. return ;
  72. }
  73. for (i=0; i<text.GetLength(); i++)
  74. {
  75. if (((text[i] == 'e') ||(text[i]=='E')) && (caret.x > i))
  76. return ;
  77. }
  78. CEdit::OnChar(nChar, nRepCnt, nFlags);
  79. }
  80. }
  81. void CNumEdit::SetValue(double val)
  82. {
  83. CString tmp;
  84. tmp.Format ("%G",val);
  85. SetWindowText (tmp);
  86. }
  87. double CNumEdit::GetValue()
  88. {
  89. CString tmp;
  90. GetWindowText (tmp);
  91. return strtod (tmp,NULL);
  92. }