How to show a progress bar in a dialog?

I tried to show the progress bar in a dialog. When OnInitDialog()  is called, Progress bar is called at the same time. But the bar cannot be shown correctly as it cannot be shown until it finishes loading the progress bar. The code is as follows:
BOOL CEncodeProgressDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here
CProgressCtrl* m_enc = (CProgressCtrl*)                            GetDlgItem(IDC_ENCPROGRESS);
      m_enc->SetRange( 0, 10 );
              m_enc->SetStep( 1 );

      for( int i = 0; i < 10; ++i )
    {
            int nSleepTime = MulDiv( 100, rand(), RAND_MAX );
            Sleep( nSleepTime );

            // 5 - Update progress control
            m_enc->StepIt();
    }
       
      return TRUE;  // return TRUE unless you set the focus                                  //to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}
makoto_liangAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sboucher789Connect With a Mentor Commented:
The way the code is written here prevents the dialog to refresh the progress bar because that dialog's thread is either sleeping (Sleep(...)) or if processing stuff (Muldiv(...)).

You could manually refresh the dialog and/or progress bar by calling m_enc->RedrawWindow() and/or this->RedrawWindow().

Otherwise, you need to put your loop in a separate thread and have this thread post user messages to your dialog so it can update the progress bar. The dialog will have plenty of time to properly update your progress bar during the sleep of the second thread.
0
 
freshmeatCommented:
wow, cool idea, i wanna know how to?
:D
0
All Courses

From novice to tech pro — start learning today.