MFC dialog box program adds pop-up window progress bar prompt

1. Create the main dialog box and pop-up dialog box

2. Add buttons to the main dialog box to perform main thread work and pop-up window display.

3. The main thread header file declares threads, main thread work functions and variables delivered with the pop-up window

    static UINT ThreadProcess(LPVOID p);//Main working thread
    int nNum;//Main thread and pop-up window delivery variable
    void AddNum();//Main thread working function

4. Function

void CTestProcessBarDlg::OnBtnStart() 
{
    // TODO: Add your control notification handler code here
    AfxBeginThread(ThreadProcess,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
    CProcessDlg pDlg;
    pDlg.pNum=&nNum;
    pDlg.DoModal();
}

UINT CTestProcessBarDlg::ThreadProcess(LPVOID p)
{
    CTestProcessBarDlg *pDlg=(CTestProcessBarDlg *)p;
    pDlg->AddNum();
    return 0;
}

void CTestProcessBarDlg::AddNum()
{

       if (nNum>=100)
        {
            break;
        }
    for (int i=0;i<=101;i++)
    {
        Sleep(100);
        nNum=i;
    }
}

5. Add Process control to the pop-up dialog

6. The header file of the pop-up dialog box

int *pNum;//Deliver pointer variable with main dialog
static UINT ThreadWinInfo(LPVOID p);//Thread operation progress bar

7. Pop-up dialog box function

BOOL CProcessDlg::OnInitDialog() //初始化中加入线程
{
    CDialog::OnInitDialog();
    
    // TODO: Add extra initialization here
    m_Process.SetRange(0,100);
    m_Process.SetStep(2);
    AfxBeginThread(ThreadWinInfo,this);
    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

 UINT CProcessDlg::ThreadWinInfo(LPVOID p)
 {      CProcessDlg *pDlg=(CProcessDlg *)p;      while(1)      {          if (*pDlg->pNum==100)          {              break;          }          pDlg->m_Process.SetPos(*pDlg- >pNum);      }      pDlg->PostMessage(WM_CLOSE);//When the variable in the main thread reaches 100, the pop-up dialog is automatically closed     return 0;  }











void CProcessDlg::OnOK() 
{
    // TODO: Add extra validation here
    *pNum=100;
}

 

Guess you like

Origin blog.csdn.net/Hat_man_/article/details/109628763