SPPPropertyGridItemColor.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /********************************************
  2. ** 工作室:S&P工作室
  3. ** 作者 :张东斌
  4. ** 日期 :2007年6月
  5. *********************************************/
  6. #include "StdAfx.h"
  7. #include "SPVC80Helpers.h"
  8. #include "SPDrawHelpers.h"
  9. #include "SPPropertyGridInplaceEdit.h"
  10. #include "SPPropertyGridInplaceButton.h"
  11. #include "SPPropertyGridInplaceList.h"
  12. #include "SPPropertyGridItem.h"
  13. #include "SPPropertyGridItemColor.h"
  14. #include "SPPropertyGrid.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CSPPropertyGridItemColor
  22. IMPLEMENT_DYNAMIC( CSPPropertyGridItemColor , CSPPropertyGridItem )
  23. CSPPropertyGridItemColor::CSPPropertyGridItemColor( CString strCaption , COLORREF clr , COLORREF * pBindColor ) : CSPPropertyGridItem( strCaption )
  24. {
  25. m_pBindColor = pBindColor;
  26. SetColor( clr );
  27. m_nFlags = SPGridItemHasEdit | SPGridItemHasExpandButton;
  28. }
  29. CSPPropertyGridItemColor::CSPPropertyGridItemColor( UINT nID , COLORREF clr , COLORREF * pBindColor ) : CSPPropertyGridItem( nID )
  30. {
  31. m_pBindColor = pBindColor;
  32. SetColor( clr );
  33. m_nFlags = SPGridItemHasEdit | SPGridItemHasExpandButton;
  34. }
  35. CSPPropertyGridItemColor::~CSPPropertyGridItemColor()
  36. {
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. //
  40. BOOL CSPPropertyGridItemColor::OnDrawItemValue( CDC & dc , CRect rcValue )
  41. {
  42. COLORREF clr = dc.GetTextColor();
  43. CRect rcSample ( rcValue.left - 2,rcValue.top + 1,rcValue.left + 18,rcValue.bottom - 1 );
  44. CSPPenDC pen ( dc,clr );
  45. CSPBrushDC brush ( dc,m_clrValue );
  46. dc.Rectangle( rcSample );
  47. CRect rcText ( rcValue );
  48. rcText.left += 25;
  49. dc.DrawText( m_strValue,rcText,DT_SINGLELINE | DT_VCENTER );
  50. return TRUE;
  51. }
  52. CRect CSPPropertyGridItemColor::GetValueRect()
  53. {
  54. CRect rcValue ( CSPPropertyGridItem::GetValueRect() );
  55. rcValue.left += 25;
  56. return rcValue;
  57. }
  58. COLORREF AFX_CDECL CSPPropertyGridItemColor::StringToRGB( CString str )
  59. {
  60. CString strRed, strGreen, strBlue;
  61. AfxExtractSubString( strRed,str,0,';' );
  62. AfxExtractSubString( strGreen,str,1,';' );
  63. AfxExtractSubString( strBlue,str,2,';' );
  64. return RGB( __min( _ttoi( strRed ),255 ),__min( _ttoi( strGreen ),255 ),__min( _ttoi( strBlue ),255 ) );
  65. }
  66. CString AFX_CDECL CSPPropertyGridItemColor::RGBToString( COLORREF clr )
  67. {
  68. CString str;
  69. str.Format( _T( "%i; %i; %i" ),GetRValue( clr ),GetGValue( clr ),GetBValue( clr ) );
  70. return str;
  71. }
  72. void CSPPropertyGridItemColor::SetValue( CString strValue )
  73. {
  74. SetColor( StringToRGB( strValue ) );
  75. }
  76. void CSPPropertyGridItemColor::SetColor( COLORREF clr )
  77. {
  78. m_clrValue = clr;
  79. if ( m_pBindColor )
  80. {
  81. *m_pBindColor = clr;
  82. }
  83. CSPPropertyGridItem::SetValue( RGBToString( clr ) );
  84. }
  85. void CSPPropertyGridItemColor::BindToColor( COLORREF * pBindColor )
  86. {
  87. m_pBindColor = pBindColor;
  88. if ( m_pBindColor )
  89. {
  90. *m_pBindColor = m_clrValue;
  91. }
  92. }
  93. void CSPPropertyGridItemColor::OnBeforeInsert()
  94. {
  95. if ( m_pBindColor && *m_pBindColor != m_clrValue )
  96. {
  97. SetColor( *m_pBindColor );
  98. }
  99. }
  100. void CSPPropertyGridItemColor::OnInplaceButtonDown()
  101. {
  102. #ifdef _INCLUDE_CONTROLS
  103. CColorDialog dlg ( m_clrValue,m_clrValue,CPS_SP_SHOW3DSELECTION | CPS_SP_SHOWHEXVALUE,m_pGrid );
  104. #else
  105. CColorDialog dlg ( m_clrValue,0,m_pGrid );
  106. #endif
  107. if ( dlg.DoModal() == IDOK )
  108. {
  109. OnValueChanged( RGBToString( dlg.GetColor() ) );
  110. m_pGrid->Invalidate( FALSE );
  111. }
  112. }