4

I'm trying to add a progress control in my dialogs, that will step upon every iteration of a loop until completion. I've never played around with progress controls before, so I'm totally clueless as to where I should start. I've added a progress control resource view into my dialog, but it just shows up as an empty progress control. I'd like to have the progress control dynamically appear/update when after pressing a button an image from somewhere stars being loaded. I am trying to add a progress control on a dialog box in Visual c++ environment.

After adding this tool the following code added to main.cpp :

void CPanoramicsampleDlg::OnNMCustomdrawProgress1(NMHDR *pNMHDR, LRESULT *pResult){
}

I can show procedure of loading in a Text control as follow:

sprintf_s(pack1,"Data%d%%",Event);
::SetWindowText(GetDlgItem(IDC_Static)->m_hWnd,pack1);

so in this way I can see loading process as %d in a text window but I don't know how to show loading procedure by progress control and how and where to define range or even progress bar handle for this progress control so on. finally I would like to know is there any function for progress control for example:

::EnableWindow(GetDlgItem(IDC_Progress1)->m_hWnd);
Braiano
  • 335
  • 6
  • 26

2 Answers2

3

The CProgressCtrl Class provides the following members that you will have to use:

  • CProgressCtrl::SetRange: Allows you to set lower and upper bound. These values ideally reflect your starting state and finishing condition.
  • CProgressCtrl::SetPos: Used to update the current position. You would update the current position where you used to output progress in your edit control.

As an alternative to calling CProgressCtrl::SetPos with an explicit position value, you can set a step increment, calling CProgressCtrl::SetStep, and update the control with a call to CProgressCtrl::StepIt. If you know the step increment ahead of time, this is an easier way to go about updating the current position.

Additional information is available at the MSDN: Using CProgressCtrl.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
3

You should first add a variable for the control, by right-clicking on the progress bar in the dialog editor, and choosing Add Variable... Your dialog class will then have an instance of a CProgressCtrl class on which you can then call the members that IInspectable has mentioned in his answer. Delete the OnNMCustomdrawProgress1 handler, you don't need it.

e.g.

m_progressCtrl.EnableWindow(TRUE);
m_progressCtrl.SetRange(0, 100);
m_progressCtrl.SetPos(75);

Then whatever that eventParam1 value is that you mention, proportion it between your start and stop value, and call SetPos with it.


Update after comment:

Do the SetRange() in the OnInitDialog() function. If you don't already have an OnInitDialog you need to override it, follow the instructions in the accepted answer of this question to do it. VS 2008, MFC: add OnInitDialog - how?

As for where you put the SetPos(): You describe that you can already track loading progress in a text control using some or other eventParam1. That sounds like a handler or callback from what ever loading you're doing, and that is where you will instead SetPos() on the progress bar instance.

Community
  • 1
  • 1
acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • I added this Variable to progress bar and I have `CProgressCtrl ProgressControl` in my public member. But I am wondering where should I add your mentioned lines like SetRange.. and how they will get sync during data loading . – Braiano May 22 '15 at 09:52
  • @Braian: If this is a compound dialog that contains both the progress bar as well as a command button, initiates the operation, it's probably best to place the call to `SetRange` in the command button message handler, immediately before the operation starts. If the dialog only contains the progress bar, `OnInitDialog` is the appropriate place. As always, if you do not know the Windows API, you stand zero chance of ever grasping MFC. You might consider taking a step back, and get intimately familiar with the Windows API first. – IInspectable May 22 '15 at 10:14
  • @acraig5075 It works but not well. It does not work according to loading procedure and just fill it out by pressing loading button. As you correctly mentioned I am trying to call that one in Event so I expected to use GetDlgItem by including progress bar ID which is IDC_PROGRESS1. Isn't it? – Braiano May 22 '15 at 12:59
  • @Braian, Perhaps it is IDC_PROGRESS1, I couldn't say, find out for sure by looking for the line in `DoDataExchange` that maps the IDC_ number you're looking for to the control variable instance. However, if you have a variable for the progress bar in the dialog, then you shouldn't need to use `GetDlgItem` because it'll return the same window pointer which you'll have to cast to a `CProgressCtrl *` which you already have as a class member variable. Also, I don't know what Event is that you speak of. – acraig5075 May 22 '15 at 13:20
  • @acraig5075 Yes I made mistake I wanted to say callback function instead of Event. Ill try to change it. Just as my last question how this progress bar get smoothly? – Braiano May 22 '15 at 13:26
  • @Braian The progress bar will animate smoothly if you call `SetPos` inside a loop, and with increasing values that start and stop with the same values you called `SetRange` with. – acraig5075 May 22 '15 at 13:37
  • @acraig5075 Ill try to modify them as well. Really appreciate you. – Braiano May 22 '15 at 13:42