MFC - Progress Control



A progress bar control is a window that an application can use to indicate the progress of a lengthy operation. It consists of a rectangle that is gradually filled, from left to right, with the system highlight color as an operation progresses. It is represented by CProgressCtrl class.

Here is the list of methods in CProgressCtrl class −

Sr.No. Name & Description
1

Create

Creates a progress bar control and attaches it to a CProgressCtrl object.

2

CreateEx

Creates a progress control with the specified Windows extended styles and attaches it to a CProgressCtrl object.

3

GetBarColor

Gets the color of the progress indicator bar for the current progress bar control.

4

GetBkColor

Gets the background color of the current progress bar.

5

GetPos

Gets the current position of the progress bar.

6

GetRange

Gets the lower and upper limits of the range of the progress bar control.

7

GetState

Gets the state of the current progress bar control.

8

GetStep

Retrieves the step increment for the progress bar of the current progress bar control.

9

OffsetPos

Advances the current position of a progress bar control by a specified increment and redraws the bar to reflect the new position.

10

SetBarColor

Sets the color of the progress indicator bar in the current progress bar control.

11

SetBkColor

Sets the background color for the progress bar.

12

SetMarquee

Turns marquee mode on or off for the current progress bar control.

13

SetPos

Sets the current position for a progress bar control and redraws the bar to reflect the new position.

14

SetRange

Sets the minimum and maximum ranges for a progress bar control and redraws the bar to reflect the new ranges.

15

SetState

Sets the state of the current progress bar control.

16

SetStep

Specifies the step increment for a progress bar control.

17

StepIt

Advances the current position for a progress bar control by the step increment (see SetStep) and redraws the bar to reflect the new position.

Let us create a new MFC application.

Step 1 − Change the ID of the Text control to and remove the caption.

Step 2 − Drag Progress Control from the Toolbox.

Step 3 − Add value variable for Static Text control.

Progress Control

Step 4 − Add control variable for the Progress control.

Progress Control

Step 5 − Here is the implementation in OnInitDialog()

BOOL CMFCProgressControlDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   // Add "About..." menu item to system menu.
 
   // IDM_ABOUTBOX must be in the system command range.
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
   ASSERT(IDM_ABOUTBOX < 0xF000);

   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != NULL) {
      BOOL bNameValid;
      CString strAboutMenu;
      bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
      ASSERT(bNameValid);
      if (!strAboutMenu.IsEmpty()) {
         pSysMenu -> AppendMenu(MF_SEPARATOR);
         pSysMenu -> AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }
   }
	
   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);      // Set big icon
   SetIcon(m_hIcon, FALSE);     // Set small icon
   
   // TODO: Add extra initialization here
   m_progCtrl.SetRange(0,100);

   for (int i = 0; i <= 100; i++) {
      m_progCtrl.SetPos(i);
   }

   return TRUE; // return TRUE unless you set the focus to a control
}

Step 6 − When the above code is compiled and executed, you will see the following output.

Progress Control
mfc_windows_controls.htm
Advertisements