home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c05 / lab07 / ex01 / progressstatusbar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.4 KB  |  143 lines

  1. // ProgressStatusBar.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "diff.h"
  6. #include "ProgressStatusBar.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CProgressStatusBar
  16.  
  17. CProgressStatusBar::CProgressStatusBar()
  18. {
  19.     m_bProgressMode = FALSE;
  20.     m_nProgressCtrlWidth = PROGRESS_CTRL_CX;
  21. }
  22.  
  23. CProgressStatusBar::~CProgressStatusBar()
  24. {
  25. }
  26.  
  27.  
  28. BEGIN_MESSAGE_MAP(CProgressStatusBar, CStatusBar)
  29.     //{{AFX_MSG_MAP(CProgressStatusBar)
  30.     ON_WM_CREATE()
  31.     ON_WM_PAINT()
  32.     //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34.  
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CProgressStatusBar message handlers
  38.  
  39. void CProgressStatusBar::SetProgressCtrlWidth(int nWidth)
  40. {
  41.     m_nProgressCtrlWidth = nWidth;
  42. }
  43.  
  44. void CProgressStatusBar::RecalcProgressDisplay()
  45. {
  46.     //    Adjust the postions of the Label and Progress Controls
  47.     //    Place the Label Control to the right of the 
  48.     //    Progress Control
  49.     //
  50.     //        [Progress Control] Label Text... 
  51.     CRect ControlRect;
  52.     CRect ClientRect;
  53.     GetClientRect(&ClientRect);
  54.     ControlRect = ClientRect; 
  55.     
  56.     //    First the Progress bar
  57.     ControlRect.left += X_MARGIN;
  58.     ControlRect.right = ControlRect.left + m_nProgressCtrlWidth;
  59.     ControlRect.top += Y_MARGIN;
  60.     ControlRect.bottom -= Y_MARGIN;
  61.     
  62.     m_ProgressCtrl.MoveWindow(ControlRect, FALSE);
  63.     
  64.     //    Then the text label using the rest of the status
  65.     //    bars client area
  66.     ControlRect.left = ControlRect.right + X_MARGIN;
  67.     ControlRect.right = ClientRect.right - X_MARGIN;
  68.  
  69.     m_ProgressLabel.MoveWindow(ControlRect, FALSE);
  70. }
  71.  
  72. void CProgressStatusBar::SetProgressLabel(LPCSTR lpszProgressLabel)
  73. {
  74.     m_ProgressLabel.SetWindowText(lpszProgressLabel);
  75.  
  76.     //  If were currently displaying progress, update 
  77.     //    placement of label and progress control
  78.     
  79.     if(m_bProgressMode)
  80.     {
  81.         RecalcProgressDisplay();
  82.         Invalidate();
  83.         UpdateWindow();
  84.     }
  85. }
  86.  
  87. void CProgressStatusBar::ShowProgressDisplay(BOOL bShow)
  88. {
  89.     m_bProgressMode = bShow;
  90.     if(m_bProgressMode)
  91.     {
  92.         RecalcProgressDisplay();
  93.     }
  94.     m_ProgressLabel.ShowWindow(m_bProgressMode ? SW_SHOW :
  95.                                                 SW_HIDE);
  96.     m_ProgressCtrl.ShowWindow (m_bProgressMode ? SW_SHOW :
  97.                                                 SW_HIDE);
  98.     Invalidate();
  99.     UpdateWindow();
  100. }
  101.  
  102. int CProgressStatusBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  103. {
  104.     if (CStatusBar::OnCreate(lpCreateStruct) == -1)
  105.         return -1;
  106.     
  107.     //     Create the Progress Control - we'll calculate its size and
  108.     //    position later - in response to a ShowProgressDisplay() call.
  109.     if(!m_ProgressCtrl.Create(    0,                 // Style - Don't Show Position or Percent
  110.                                 CRect(0,0,0,0),    // Initial position
  111.                                 this,            // Parent
  112.                                 0))                // Child ID
  113.     {
  114.         return -1;
  115.     }
  116.  
  117.     //     Create the Progress Label - we'll calculate its size and
  118.     //    position later - in response to a ShowProgressDisplay() call.
  119.     if(!m_ProgressLabel.Create(    NULL,                // Text
  120.                                 WS_CHILD|SS_LEFT,     // Style
  121.                                 CRect(0,0,0,0),        // Initial position
  122.                                 this))                // Parent
  123.     {
  124.         return -1;
  125.     }
  126.     
  127.     //    Use the same font as the Status Bar
  128.     m_ProgressLabel.SetFont(GetFont());
  129.     
  130.     return 0;
  131. }
  132.  
  133. void CProgressStatusBar::OnPaint() 
  134. {
  135.     //    If we were displaying the progress control, then we
  136.      //    need to handle painting of the Status Bar,
  137.     //    otherwise defer to the base class
  138.     if(!m_bProgressMode)
  139.     {
  140.         CStatusBar::OnPaint();
  141.     }
  142. }
  143.