ProgBar.cpp
Upload User: nhyuejuan
Upload Date: 2013-12-02
Package Size: 171k
Code Size: 2k
Category:

StatusBar

Development Platform:

Visual C++

  1. ////////////////////////////////////////////////////////////////
  2. // MSDN Magazine -- January 2003
  3. // If this code works, it was written by Paul DiLascia.
  4. // If not, I don't know who wrote it.
  5. // Compiles with VC 6.0 or VS.NET on Windows XP. Tab size=3.
  6. //
  7. #include "StdAfx.h"
  8. #include "ProgBar.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. IMPLEMENT_DYNAMIC(CProgStatusBar, CStatusBar)
  15. BEGIN_MESSAGE_MAP(CProgStatusBar, CStatusBar)
  16. ON_WM_CREATE()
  17. ON_WM_SIZE()
  18. END_MESSAGE_MAP()
  19. CProgStatusBar::CProgStatusBar()
  20. {
  21. }
  22. CProgStatusBar::~CProgStatusBar()
  23. {
  24. }
  25. /////////////////
  26. // Status bar created: create progress bar too.
  27. //
  28. int CProgStatusBar::OnCreate(LPCREATESTRUCT lpcs)
  29. {
  30. lpcs->style |= WS_CLIPCHILDREN;
  31. VERIFY(CStatusBar::OnCreate(lpcs)==0);
  32. VERIFY(m_wndProgBar.Create(WS_CHILD, CRect(), this, 1));
  33. m_wndProgBar.SetRange(0,100);
  34. return 0;
  35. }
  36. //////////////////
  37. // Status bar was sized: adjust size of progress bar to same as first
  38. // pane (ready message). Note that the progress bar may or may not be
  39. // visible.
  40. //
  41. void CProgStatusBar::OnSize(UINT nType, int cx, int cy)
  42. {
  43. CStatusBar::OnSize(nType, cx, cy); // call base class
  44. CRect rc;   // rectangle 
  45. GetItemRect(0, &rc);   // item 0 = first pane, "ready" message
  46. m_wndProgBar.MoveWindow(&rc,FALSE);// move progress bar
  47. }
  48. //////////////////
  49. // Set progress bar position. pct is an integer from 0 to 100:
  50. //
  51. //  0 = hide progress bar and display ready message (done);
  52. // >0 = (assemed 0-100) set progress bar position
  53. //
  54. // You should call this from your main frame to update progress.
  55. // (See Mainfrm.cpp)
  56. //
  57. void CProgStatusBar::OnProgress(UINT pct)
  58. {
  59. CProgressCtrl& pc = m_wndProgBar;
  60. DWORD dwOldStyle = pc.GetStyle();
  61. DWORD dwNewStyle = dwOldStyle;
  62. if (pct>0)
  63. // positive progress: show prog bar
  64. dwNewStyle |= WS_VISIBLE;
  65. else
  66. // prog <= 0: hide prog bar
  67. dwNewStyle &= ~WS_VISIBLE;
  68. if (dwNewStyle != dwOldStyle) {
  69. // change state of hide/show
  70. SetWindowText(NULL); // clear old text
  71. SetWindowLong(pc.m_hWnd, GWL_STYLE, dwNewStyle); // change style
  72. }
  73. // set progress bar position
  74. pc.SetPos(pct);
  75. if (pct==0)
  76. // display MFC idle (ready) message.
  77. GetParent()->PostMessage(WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);
  78. }