HomeC&C++WainTutorialsSamplesTip&TrickTools


To show the user that the program is working, you can use a progress control.
On the calculator this could simulate that the program is calculating.
First to create the progress control:
      CreateWindow(PROGRESS_CLASS,
                   "",
                   WS_CHILD | WS_VISIBLE | PBS_SMOOTH,
                   5, 100, 100, 20,
                   hwndDlg,
                   (HMENU )ProgressCtrlId,
                   InstanceHandle,
                   0);
Then we will set the range in which the control can progress:
SendDlgItemMessage(hwndDlg,
                   ProgressCtrlId,
                   PBM_SETRANGE,
                   0,
                   MAKELPARAM(0, 50));
This will set the minimum pos to 0 and max to 50.
Now we will wait for the user to press the Calculate button, when he do, we will show the first progress, and start a timer, in stead of the usual calculation:
       case WM_COMMAND:
          if(HIWORD(wParam) == BN_CLICKED &&
             LOWORD(wParam) == ButtonId)
      {
         if(!TimerRunning)
         {
            TimerIdx = 1;
            TimerRunning = true;
            SetTimer(hwndDlg, ProgressTimer, 100, 0);
            SendDlgItemMessage(hwndDlg,
                               ProgressCtrlId,
                               PBM_SETPOS,
                               TimerIdx, 0);
         }
      }
Now when the timer expires, we will update the progress bar, and if our counter reach 50, the max range for the progress bar, we will do the calculations:
   case WM_TIMER:
      TimerIdx++;
      SendDlgItemMessage(hwndDlg,
                         ProgressCtrlId,
                         PBM_SETPOS,
                         TimerIdx,
                         0);
      if(TimerIdx > 50)
      {
         KillTimer(hwndDlg, ProgressTimer);
         TimerRunning = false;
         // Do normal calculations
And thats it. The complete code is here.