MFC non-modal add-in program method one (threading method)

Source: Internet
Author: User

Because the non-modal dialog itself does not have a message loop, message processing cannot be done after it is created. The message loop needs to be shared with the parent window. If the control is separate in a child window because it does not have a separate message loop, the update is not possible.

If the control is updated in the parent window, it causes the program to feign animation. If the following code updates the Subwindow message in the main window, the interface enters the suspended animation state. Because the main realm has sleep (100) facing the main process, as shown in the following code

voidCmodelessdlg::onbnclickedok () {dlgmodeless*pdlg =Newdlgmodeless (); Pdlg-Create (idd_dg_modless, NULL); Pdlg-ShowWindow (sw_show); Pdlg->m_progress1.setrange (1, -); Pdlg->m_progress1.setstep (1);  for(inti =0; i< -; i++) {Pdlg-M_progress1.stepit (); Sleep ( -); }    //TODO: Add control notification handler code here//Cdialogex::onok ();}
View Code

Try to open a thread in the main window to update the control

void Cmodelessdlg::onbnclickedok () {    0, thread_priority_idle);     // TODO: Add control notification handler    code here // Cdialogex::onok ();}

UINT Mythreadexample (LPVOID *pparam) {dlgmodeless*pdlg =Newdlgmodeless (); Pdlg-Create (idd_dg_modless, NULL); Pdlg-ShowWindow (sw_show); Pdlg->m_progress1.setrange (1, -); Pdlg->m_progress1.setstep (1);  for(inti =0; i< -; i++) {Pdlg-M_progress1.stepit (); Sleep ( -); }    return 0;}

Found such a thread, the child window directly suspended animation can not move!

I find that it seems a bit foolish to open a window directly in a sub-thread, which seems to be to let the whole program run in the Subwindow, while sleep (100) and above in the main program update, the same causes the program suspended animation. And is suspended in the child window of death.

In order not to be so foolish, I changed the program as follows. A pointer to a global modal window is created, and the window is created inside the constructor, and then responds at the button event. Hidden at the thread. This avoids creating child windows multiple times.

Dlgmodeless *pdlg =NewDlgmodeless ();//creates a new, non-modal window pointer globally. //Cmodelessdlg dialog BoxUINT Mythreadexample (LPVOID *pparam) {Pdlg->m_progress1.setrange (1, -); Pdlg->m_progress1.setstep (1);  for(inti =0; i< -; i++) {Pdlg-M_progress1.stepit (); Sleep ( -); } Pdlg ->showwindow (FALSE); return 1;} Cmodelessdlg::cmodelessdlg (CWnd* pparent/*=nullptr*/): CDialogEx (Idd_modeless_dialog, pparent) {M_hicon= AfxGetApp ()LoadIcon (IDR_MAINFRAME); Pdlg->create (idd_dg_modless, NULL);//constructor inside Create program, button response, avoid multiple creation}
void Cmodelessdlg::onbnclickedok ()//Key response event {    //if (Pdlg->getdlgitem (idd_dg_modless));    cwnd* pWnd = GetDlgItem (idd_dg_modless);        Pdlg, ShowWindow (sw_show);     0 , Thread_priority_idle);     // TODO: Add control notification handler    code here // Cdialogex::onok ();}

After this processing, I can easily update the process control in the non-modal window.

You can move the child window at the same time as the new.

After the update is complete, the thread function is hidden by Pdlg->showwindow (false).

MFC non-modal add-in method One (threading method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.