FloatEdit.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 4k
Category:

Graph program

Development Platform:

Visual C++

  1. // FloatEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FloatEdit.h"
  5. #include ".floatedit.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CFloatEdit
  13. CFloatEdit::CFloatEdit()
  14. {
  15. m_fix_button = NULL;
  16. }
  17. CFloatEdit::~CFloatEdit()
  18. {
  19. }
  20. void  CFloatEdit::SetValue(float val)
  21. {
  22. if (!IsFixed())
  23. {
  24. CString tmpS;
  25. tmpS.Format("%f",val);
  26. SetWindowText(tmpS);
  27. GetParent()->SetFocus();
  28. SendMessage(GET_DIALOGS_MESSAGE_POST_SET_FOCUS);
  29. }
  30. }
  31. float CFloatEdit::GetValue()
  32. {
  33. CString tmpS;
  34. GetWindowText(tmpS);
  35. char   *str,*stopstring;
  36. str = tmpS.GetBuffer();
  37. float  x;
  38. x = (float)strtod( str, &stopstring );
  39. if(str==stopstring)
  40. {
  41. SetWindowText("0.0000");
  42. return 0.0f;
  43. }
  44. return x;
  45. }
  46. void  CFloatEdit::SetFixButton(CToolTipBitmapButton* fB)
  47. {
  48. ASSERT(fB);
  49. m_fix_button = fB;
  50. }
  51. bool  CFloatEdit::IsFixed()
  52. {
  53. if(!m_fix_button) return false;
  54. return (m_fix_button->IsPressed());
  55. }
  56. BEGIN_MESSAGE_MAP(CFloatEdit, CEdit)
  57. //{{AFX_MSG_MAP(CFloatEdit)
  58. ON_WM_CHAR()
  59. //}}AFX_MSG_MAP
  60. ON_WM_SETFOCUS()
  61. ON_MESSAGE(GET_DIALOGS_MESSAGE_POST_SET_FOCUS, OnPostSetFocus)
  62. ON_MESSAGE(GET_DIALOGS_MESSAGE_PRE_SET_FOCUS, OnPreSetFocus)
  63. ON_WM_KEYDOWN()
  64. END_MESSAGE_MAP()
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CFloatEdit message handlers
  67. void CFloatEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  68. {
  69. /*switch(nChar)
  70.     { 
  71. case _T('.'):
  72. case _T('0'):
  73. case _T('1'):
  74. case _T('2'):
  75. case _T('3'):
  76. case _T('4'):
  77. case _T('5'):
  78. case _T('6'):
  79. case _T('7'):
  80. case _T('8'):
  81. case _T('9'):
  82. case _T('b'): //backspace 
  83. if(m_fix_button)
  84. m_fix_button->SetPressed(true);
  85. break;
  86. default:
  87. //GetParent()->SendMessage(WM_CHAR,nChar,0);
  88. return;
  89.        } 
  90. CEdit::OnChar(nChar,nRepCnt, nFlags);*/
  91. if(!(nChar >= '0' && nChar <= '9' || nChar == '.' || nChar == 'b' || nChar=='-'))
  92. return;
  93. if(nChar >= '0' && nChar <= '9' && m_fix_button)
  94. m_fix_button->SetPressed(true);
  95. CString str;
  96. GetWindowText(str);
  97. if(nChar == '.' && (str.Find('.') >= 0 || str.IsEmpty()))
  98. return;
  99. int nStartChar, nEndChar;
  100. GetSel(nStartChar, nEndChar);
  101. if(nChar == '-' && (nStartChar != 0)/* || nEndChar != 0)*/)
  102. return;
  103. if(nChar == 'b' && nStartChar <= 0)
  104. return;
  105. CEdit::OnChar(nChar, nRepCnt, nFlags);
  106. }
  107. void CFloatEdit::OnSetFocus(CWnd* pOldWnd)
  108. {
  109. CEdit::OnSetFocus(pOldWnd);
  110. SendMessage(GET_DIALOGS_MESSAGE_POST_SET_FOCUS);
  111. }
  112. LRESULT CFloatEdit::OnPostSetFocus(WPARAM, LPARAM)
  113. {
  114. SetSel(0,-1);
  115. return 0L;
  116. }
  117. LRESULT CFloatEdit::OnPreSetFocus(WPARAM nChar, LPARAM)
  118. {
  119. SetFocus();
  120. switch(nChar)
  121. case _T('-'):
  122. case _T('0'):
  123. case _T('1'):
  124. case _T('2'):
  125. case _T('3'):
  126. case _T('4'):
  127. case _T('5'):
  128. case _T('6'):
  129. case _T('7'):
  130. case _T('8'):
  131. case _T('9'):
  132. if(m_fix_button)
  133. m_fix_button->SetPressed(true);
  134. SetWindowText((char*)&nChar);
  135. SetSel(1,2);
  136. break;
  137. return 0L;
  138. }
  139. void CFloatEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  140. {
  141. /*switch(nChar)
  142. //case _T('.'):
  143. case _T('0'):
  144. case _T('1'):
  145. case _T('2'):
  146. case _T('3'):
  147. case _T('4'):
  148. case _T('5'):
  149. case _T('6'):
  150. case _T('7'):
  151. case _T('8'):
  152. case _T('9'):
  153. case _T('b'): //backspace 
  154. case VK_DELETE:
  155. if(m_fix_button)
  156. m_fix_button->SetPressed(true);
  157. break;
  158. case VK_LEFT:
  159. case VK_RIGHT:
  160. break;
  161. default:
  162. return;
  163. */
  164. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  165. }