home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / percba / percbar.cpp next >
C/C++ Source or Header  |  1994-04-30  |  4KB  |  147 lines

  1. ///////////////////////////////////////////////////////////////////
  2. // class for a "percentbar" - use it with the "MFC's"
  3. //
  4. // Norbert Gutbrod (Germany) - [100265,2327]
  5. // Ver 1.0 / 04/30/94
  6. ///////////////////////////////////////////////////////////////////
  7.  
  8. ///////////////////////////////////////////////////////////////////
  9. // It can be used in a dialog, which contains a "Black Frame" and
  10. // - if you want - a "Static Text"
  11. //
  12. // 1. Put it into your project
  13. // 2. "#include" the header in your source
  14. // 3. Construct the object
  15. // 4. if you didn't use the 2nd constructor -> subclass it
  16. // 5. put the "UpdatePercentBar()" in your "OnPaint" routine
  17. // 6. call the "SetPercent()"...
  18. // that's all
  19. ///////////////////////////////////////////////////////////////////
  20.  
  21. #include <afxwin.h>
  22. #include "percbar.h"
  23.  
  24. // 1st constructor
  25. CPercentBar::CPercentBar()
  26. {
  27.     m_pParent        = NULL;
  28.     m_nFrameID       = 0;
  29.     m_nTextID        = 0;
  30.     m_CurrentPercent = 0;
  31.     m_Color          = 0x000000FF;
  32. }
  33.  
  34. // 2nd constructor
  35. CPercentBar::CPercentBar( UINT nFrameID, UINT nTextID, CWnd *pParent )
  36. {
  37.     m_pParent        = pParent;
  38.     m_nFrameID       = nFrameID;
  39.     m_nTextID        = nTextID;
  40.     m_CurrentPercent = 0;
  41.     m_Color          = 0x000000FF;
  42.     m_Frame.Attach( m_pParent->GetDlgItem( nFrameID )->m_hWnd );
  43.     
  44.     GetSize();
  45. }
  46.     
  47. // destructor
  48. CPercentBar::~CPercentBar()
  49. {
  50.     if( m_pParent != NULL )
  51.         m_Frame.Detach();
  52. }
  53.  
  54. // subclass the frame
  55. void CPercentBar::SubclassDlgItem( UINT nFrameID, UINT nTextID, CWnd *pParent )
  56. {
  57.     m_pParent  = pParent;
  58.     m_nFrameID = nFrameID;
  59.     m_nTextID  = nTextID;
  60.     m_Frame.Attach( m_pParent->GetDlgItem( nFrameID )->m_hWnd );
  61.     
  62.     GetSize();
  63. }
  64.  
  65. // set color for the bar
  66. void CPercentBar::SetColor( COLORREF crColor )
  67. {
  68.     m_Color = crColor;
  69. }
  70.  
  71. // update (redraw) the bar
  72. void CPercentBar::UpdatePercentBar( void )
  73. {
  74.     RECT rTemp;
  75.     
  76.     memcpy( &rTemp, &m_rBorder, sizeof( rTemp ) );
  77.     rTemp.right = GetNewPos( m_CurrentPercent );
  78.     DrawPercentBar( rTemp );
  79. }
  80.  
  81. // set new percent-value and update it
  82. void CPercentBar::SetPercent( int nPercent )
  83. {
  84.     if( nPercent != m_CurrentPercent )
  85.         {
  86.         RECT rTemp;
  87.         
  88.         memcpy( &rTemp, &m_rBorder, sizeof( rTemp ) );
  89.         if( m_CurrentPercent > 0 )
  90.             m_CurrentPercent--;
  91.         rTemp.left = GetNewPos( m_CurrentPercent );
  92.         rTemp.right = GetNewPos( nPercent );
  93.         m_CurrentPercent = nPercent;
  94.         DrawPercentBar( rTemp );
  95.         }
  96. }    
  97.  
  98. // get the client-area
  99. void CPercentBar::GetSize( void )
  100. {
  101.     m_Frame.GetClientRect( &m_rBorder );
  102.     m_rBorder.left++;
  103.     m_rBorder.top += 2;
  104.     m_rBorder.right -= 2;
  105.     m_rBorder.bottom--;
  106.     m_fWidth = m_rBorder.right - m_rBorder.left;
  107. }
  108.  
  109. // get the position of the current percent-value
  110. int CPercentBar::GetNewPos( int iPercent )
  111. {
  112.     return (int)((m_fWidth / 100) * (float)iPercent) + 2;
  113. }
  114.  
  115. // draw the bar
  116. void CPercentBar::DrawPercentBar( RECT &rDraw )
  117. {
  118.     CDC    *dc = m_Frame.GetDC();
  119.     CBrush myBrush( m_Color );
  120.     CBrush *pOldBrush;
  121.     CPen   myPen( PS_NULL, 0, 0 );
  122.     CPen   *pOldPen;
  123.     char   szString[20];
  124.     
  125.     // select the brush and pen
  126.     pOldBrush = dc->SelectObject( &myBrush );
  127.     pOldPen = dc->SelectObject( &myPen );
  128.     
  129.     // draw it...
  130.     dc->Rectangle( &rDraw );
  131.     
  132.     // select old brush
  133.     dc->SelectObject( pOldBrush );
  134.     dc->SelectObject( pOldPen );
  135.     myBrush.DeleteObject();
  136.     myPen.DeleteObject();
  137.     
  138.     m_Frame.ReleaseDC( dc );
  139.     
  140.     // set the text
  141.     if( m_nTextID != 0 )
  142.         {
  143.         sprintf( szString, "%d %%", m_CurrentPercent );
  144.         m_pParent->SetDlgItemText( m_nTextID, szString );
  145.         }
  146. }
  147.