PropertyGridItemDouble.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "stdafx.h"
  2. #include "PropertyGridInplaceEdit.h"
  3. #include "PropertyGridInplaceButton.h"
  4. #include "PropertyGridInplaceList.h"
  5. #include "PropertyGridItem.h"
  6. #include "PropertyGridItemDouble.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CPropertyGridItemDouble::CPropertyGridItemDouble(CString strCaption, double value, int precision)
  11. : CPropertyGridItem(strCaption)
  12. {
  13. _Init();
  14. m_nPrecision = precision;
  15. SetDouble(value);
  16. }
  17. CPropertyGridItemDouble::CPropertyGridItemDouble(UINT nID, double value, int precision)
  18. : CPropertyGridItem(nID)
  19. {
  20. _Init();
  21. m_nPrecision = precision;
  22. SetDouble(value);
  23. }
  24. CPropertyGridItemDouble::~CPropertyGridItemDouble()
  25. {
  26. }
  27. void CPropertyGridItemDouble::_Init()
  28. {
  29. m_pBindDouble = NULL;
  30. m_bHasMinValue = FALSE;
  31. m_bHasMaxValue = FALSE;
  32. m_fMinValue = 0;
  33. m_fMaxValue = 0;
  34. }
  35. void CPropertyGridItemDouble::SetDouble(double value)
  36. {
  37. m_fValue = value;
  38. if (m_bHasMinValue && m_fValue < m_fMinValue)
  39. m_fValue = m_fMinValue;
  40. if (m_bHasMaxValue && m_fValue > m_fMaxValue)
  41. m_fValue = m_fMaxValue;
  42. CString strFormat; //¸ñʽ×Ö·û´®
  43. strFormat.Format( _T("%%.%df"), m_nPrecision);
  44. CString strValue;
  45. strValue.Format(strFormat, m_fValue);
  46. CPropertyGridItem::SetValue(strValue);
  47. if (m_pBindDouble)
  48. *m_pBindDouble = m_fValue;
  49. }
  50. double CPropertyGridItemDouble::GetDouble()
  51. {
  52. return m_fValue;
  53. }
  54. void CPropertyGridItemDouble::SetValue(CString strValue)
  55. {
  56. #ifdef _UNICODE
  57. char ansi_string[256];
  58. ::WideCharToMultiByte(CP_ACP, 0, strValue, -1, ansi_string, 256, NULL, NULL);
  59. SetDouble(atof(ansi_string));
  60. #else
  61. SetDouble(atof(strValue));
  62. #endif
  63. }
  64. void CPropertyGridItemDouble::BindToDouble(double* pBindDouble)
  65. {
  66. m_pBindDouble = pBindDouble;
  67. if (m_pBindDouble)
  68. SetDouble(*m_pBindDouble);
  69. }
  70. void CPropertyGridItemDouble::SetMinDouble(double fMinValue)
  71. {
  72. m_bHasMinValue = TRUE;
  73. m_fMinValue = fMinValue;
  74. if (m_fValue < m_fMinValue)
  75. SetDouble(m_fMinValue);
  76. }
  77. void CPropertyGridItemDouble::SetMaxDouble(double fMaxValue)
  78. {
  79. m_bHasMaxValue = TRUE;
  80. m_fMaxValue = fMaxValue;
  81. if (m_fValue > m_fMaxValue)
  82. SetDouble(m_fMaxValue);
  83. }