Filler.cpp
上传用户:xrc9201
上传日期:2013-02-04
资源大小:35k
文件大小:5k
源码类别:

源码/资料

开发平台:

Visual C++

  1. #include "Stdafx.h"
  2. #include "Filler.h"
  3. // CFiller - Implementation
  4. CFiller::CFiller()
  5. {
  6. m_csText = _T("");
  7. m_nMinVal = 0;
  8. m_nMaxVal = 100;
  9. m_nPos = 0;
  10. m_FillerColor = RGB( 0, 0, 255 );
  11. m_FillerBkColor = RGB( 255, 0, 0 );
  12. m_FillerTextColor = RGB( 255, 255, 255 );
  13. }
  14. CFiller::~CFiller()
  15. {
  16. }
  17. INT CFiller::SetFillerText( LPCSTR lpszText )
  18. {
  19. // Step 1 - Set Filler Text
  20. m_csText = lpszText;
  21. return SUCCESS;
  22. }
  23. INT CFiller::SetFillerRange( INT nMinVal, INT nMaxVal )
  24. {
  25. // Step 1 - Set MinVal and MaxVal after validating
  26. if( nMaxVal <= nMinVal )
  27. {
  28. return FAILURE;
  29. }
  30. m_nMinVal = nMinVal;
  31. m_nMaxVal = nMaxVal;
  32. return SUCCESS;
  33. }
  34. INT CFiller::GetFillerPos()
  35. {
  36. // Step 1 - Return m_nPos
  37. return m_nPos;
  38. }
  39. INT CFiller::SetFillerPos( INT nPos )
  40. {
  41. // Step 1 - Validate pos before setting
  42. if( nPos < m_nMinVal || nPos > m_nMaxVal )
  43. {
  44. return FAILURE;
  45. }
  46. // Step 2 - Store old filler pos
  47. INT nOldPos = m_nPos;
  48. // Step 3 - Set m_nPos
  49. m_nPos = nPos;
  50. // Step 4 - Return old filler pos
  51. return nOldPos;
  52. }
  53. COLORREF CFiller::SetFillerColor( COLORREF & rFillerColor )
  54. {
  55. // Step 1 - Store old color
  56. COLORREF OldFillerColor = m_FillerColor;
  57. // Step 2 - Set m_FillerColor
  58. m_FillerColor = rFillerColor;
  59. // Step 3 - Return old filler color
  60. return OldFillerColor;
  61. }
  62. COLORREF CFiller::SetFillerBkColor( COLORREF & rFillerBkColor )
  63. {
  64. // Step 1 - Store old bk color
  65. COLORREF OldFillerBkColor = m_FillerBkColor;
  66. // Step 2 - Set m_FillerBkColor
  67. m_FillerBkColor = rFillerBkColor;
  68. // Step 3 - Return old filler bk color
  69. return OldFillerBkColor;
  70. }
  71. COLORREF CFiller::SetFillerTextColor( COLORREF & rFillerTextColor )
  72. {
  73. // Step 1 - Store old text color
  74. COLORREF OldFillerTextColor = m_FillerTextColor;
  75. // Step 2 - Set m_FillerTextColor
  76. m_FillerTextColor = rFillerTextColor;
  77. // Step 3 - Return old filler text color
  78. return OldFillerTextColor;
  79. }
  80. // CLtoRFiller - Implementation
  81. CLToRFiller::CLToRFiller() : CFiller()
  82. {
  83. }
  84. CLToRFiller::~CLToRFiller()
  85. {
  86. }
  87. INT CLToRFiller::DoFill( CWnd * pWnd, const CRect & rRect )
  88. {
  89. CRect Rect = rRect;
  90. // Step 1 - Validate window pointer
  91. ASSERT( IsWindow( pWnd->m_hWnd ) );
  92. // Step 2 - Calculate Fill Area
  93. INT nRight = 0;
  94. nRight = ( rRect.Width() * m_nPos ) / ( m_nMaxVal - m_nMinVal );
  95. CRect FillArea( rRect.left, rRect.top, nRight, rRect.bottom );
  96. CRect RemainingArea( nRight, rRect.top, rRect.right, rRect.bottom );
  97. CDC * pDC = pWnd->GetDC();
  98. pDC->FillSolidRect( FillArea, m_FillerColor );
  99. pDC->FillSolidRect( RemainingArea, m_FillerBkColor );
  100. // Step 3 - Display text provided or the default %
  101. if( m_csText.IsEmpty() == FALSE )
  102. {
  103. pDC->SetBkMode( TRANSPARENT );
  104. pDC->SetTextColor( m_FillerTextColor );
  105. pDC->DrawText( m_csText, m_csText.GetLength(), 
  106. Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE ); 
  107. }
  108. else if( m_nPos > 0 )
  109. {
  110. CHAR szText[ 512 ];
  111. memset( szText, 0, sizeof( szText ) );
  112. INT nPercent = ( INT )( ( ( double ) m_nPos / ( double ) m_nMaxVal ) * 100 );
  113. sprintf( szText, "%d%%", nPercent );
  114. pDC->SetBkMode( TRANSPARENT );
  115. pDC->SetTextColor( m_FillerTextColor );
  116. pDC->DrawText( szText, strlen( szText ), 
  117. Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE ); 
  118. }
  119. pWnd->ReleaseDC( pDC );
  120. return SUCCESS;
  121. }
  122. // CRToLFiller - Implementation
  123. CRToLFiller::CRToLFiller() : CFiller()
  124. {
  125. }
  126. CRToLFiller::~CRToLFiller()
  127. {
  128. }
  129. INT CRToLFiller::DoFill( CWnd * pWnd, const CRect & rRect )
  130. {
  131. CRect Rect = rRect;
  132. // Step 1 - Validate window pointer
  133. ASSERT( IsWindow( pWnd->m_hWnd ) );
  134. // Step 2 - Calculate Fill Area
  135. INT nLeft = 0;
  136. nLeft = ( rRect.Width() * m_nPos ) / ( m_nMaxVal - m_nMinVal );
  137. nLeft = rRect.Width() - nLeft;
  138. CRect FillArea( nLeft, rRect.top, rRect.right, rRect.bottom );
  139. CRect RemainingArea( rRect.left, rRect.top, nLeft, rRect.bottom );
  140. CDC * pDC = pWnd->GetDC();
  141. pDC->FillSolidRect( FillArea, m_FillerColor );
  142. pDC->FillSolidRect( RemainingArea, m_FillerBkColor );
  143. // Step 3 - Display text provided or the default %
  144. if( m_csText.IsEmpty() == FALSE )
  145. {
  146. pDC->SetBkMode( TRANSPARENT );
  147. pDC->SetTextColor( m_FillerTextColor );
  148. pDC->DrawText( m_csText, m_csText.GetLength(), 
  149. Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE ); 
  150.   }
  151. else if( m_nPos > 0 )
  152. {
  153. CHAR szText[ 512 ];
  154. memset( szText, 0, sizeof( szText ) );
  155. INT nPercent = ( INT )( ( ( double ) m_nPos / ( double ) m_nMaxVal ) * 100 );
  156. sprintf( szText, "%d%%", nPercent );
  157. pDC->SetBkMode( TRANSPARENT );
  158. pDC->SetTextColor( m_FillerTextColor );
  159. pDC->DrawText( szText, strlen( szText ), 
  160. Rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER | DT_SINGLELINE ); 
  161. }
  162. pWnd->ReleaseDC( pDC );
  163. return SUCCESS;
  164. }