"Go" MFC Interface Update Implementation method

Source: Internet
Author: User

Original URL: http://www.cnblogs.com/skywatcher/p/3572311.html

1. Update window

That is , the UpdateWindow () function immediately sends the WM_PAINT message to update the entire window.

void Cedittestdlg::onbnclickedbtnsysupdate () {    CString cstmp;    int i = 0;    while (I <)    {        Sleep];        i + = 1;        Cstmp.format (_t ("%d"), I);        m_value = cstmp;//cannot update only show results        //m_editnum.setwindowtext (cstmp);//cannot update or display results        updatedata (FALSE);        UpdateWindow ();//Can be updated but operation again will be stuck        //invalidate (FALSE);//cannot update only show results        //redrawwindow (null,null,rdw_invalidate | Rdw_updatenow | Rdw_erase);//rdw_invalidate and invalidate () effect, rdw_erase cause flicker    }}

However, this can cause the interface to suspend animation, and the update process cannot do anything to the window. Control variables for controls are sometimes not updated Invalidate () is generally used in multi-document dialogs, andRedrawWindow () is the first two functions to be called, in more ways. For details see: MFC Redraw function

2. Timer implementation

Call the SetTimer () start timer in the location where it needs to be updated, handle the update in the timer response function OnTimer () , and enable multiple timers to update multiple locations at the same time.

Start Timer:

void Cedittestdlg::onbnclickedbtntimerupdate () {    this->settimer (1,1,null);    This->settimer (2,1,null);}

Timer Internal processing:

void Cedittestdlg::ontimer (Uint_ptr nidevent) {    switch (nidevent)    {case    1:        if (tmp >=)        {            This->killtimer (1);            MessageBox (_t ("Timer 1 Stop!") "), null, NULL);            return;        }        Sleep (a);        TMP + = 1;        M_value. Format (_t ("%d"), TMP);        UpdateData (FALSE);        break;    Case 2:        if (tmp1 >=)        {            This->killtimer (2);            MessageBox (_t ("Timer 2 stop!") "), null, NULL);            return;        }        Sleep (a);        TMP1 + = 1;        M_value2. Format (_t ("%d"), TMP1);        UpdateData (FALSE);    Default: Break        ;    }    Cdialog::ontimer (nidevent);}

Through the timer interface will not be suspended animation, the realization is simple and clear.

3. Thread Update

Create a new thread to update the interface and create the thread:

void Cedittestdlg::onbnclickedbtnthreadupdate () {    cwinthread* pThread;    PThread = AfxBeginThread (updatethread,this);}

Thread Internal implementation:

Static UINT Updatethread (LPVoid lpparam) {    Cedittestdlg *dlg = (cedittestdlg*) Lpparam;    int i = 0;    while (I <)    {        Sleep];        i + = 1;        Dlg->m_value2. Format (_t ("%d"), I);        Dlg->updatedata (FALSE);//release feasible        dlg->m_editctl.setwindowtext (dlg->m_value2);//Use control variable        //dlg->getdlgitem (idc_edit2)->setwindowtext (dlg->m_value2);    }    return 0;}

Here, it is best not to use UpdateData (FALSE) to display to the interface, the debug next to the crash, and release mode without any problems, see: Thread call UpdateData function error

4. Send custom message updates in threads

Send a custom update interface message to the window in the thread and let the message join the system Message queue for the purpose of updating the interface. Custom messages can be referenced by: MFC adds custom messages

void Cedittestdlg::onbnclickedbtnmsgupdate () {    cwinthread* pThread;    PThread = AfxBeginThread (sendmsgthread,this);}
Static UINT Sendmsgthread (LPVoid lpparam) {    Cedittestdlg *dlg = (cedittestdlg*) Lpparam;    int i = 0;    while (I <)    {        Sleep];        i + = 1;        Dlg->m_value2. Format (_t ("%d"), I);        PostMessage (dlg->m_hwnd,wm_updatedata,false,null);        SendMessage (dlg->m_hwnd,wm_updatedata,false,null);        SendMessageTimeout (Dlg->m_hwnd, Wm_updatedata, False,null, Smto_block, N, NULL);    }    return 0;}

PostMessage (),SendMessage ( ) and sendmessagetimeout () can be implemented when sending a message, Specific differences See: SendMessage and PostMessage differences

5. Device Drawing Updates

The process of drawing changes directly in the interface can also result in suspended animation.

void Cedittestdlg::onbnclickedbtndrawupdate () {    CString cstmp;    int i = 0;    cdc* PDC = This->getdc ();    while (I <)    {        Sleep];        i + = 1;        Cstmp.format (_t ("%d"), I);        Pdc->textout (+, cstmp);    }    ReleaseDC (PDC);}

6. Static Control update

Assigning a value update to a static control can also cause suspended animation. (It is worth mentioning that the same method can be used to update the Eidt control in VC6.0, but it is not possible to test now, but it is hoped that experienced people will give guidance.) )

void Cedittestdlg::onbnclickedbtnstaticupdate () {    CString cstmp;    int i = 0;    while (I <)    {        Sleep];        i + = 1;        Cstmp.format (_t ("%d"), I);        M_csstaticnum = cstmp;//can be updated but again operation will be stuck        //m_staticctlnum.setwindowtext (cstmp);//Do not update nor display results        UpdateData (FALSE );    }}

7. Send System Message Update

The WM_PAINT message is sent directly to the system message loop to update the interface, but the same is true of suspended animation, so you can create a function doevent () implementation: When a message such as a drag-and-click is paused, it takes precedence over the system message to process the message we send.

void Cedittestdlg::onbnclickedbtnmsg () {    CString cstmp;    int i = 0;    while (I <)    {        Sleep];        i + = 1;        Cstmp.format (_t ("%d"), I);        M_value = cstmp;//can be updated but again operation will be stuck        //m_editnum.setwindowtext (cstmp);//cannot be updated or display results        updatedata (FALSE);        GetDlgItem (IDC_EDIT1)->sendmessage (WM_PAINT);//Can be updated but again operation will die        DoEvents ();//Turn out processing system message    }}
A system message is paused when processing system messages void DoEvents () {    MSG msg;//define a variable of MSG type while    (PeekMessage (&msg,null,0,0,pm_remove))/ /Gets the message and removes the message from the message queue (preventing duplicate responses).    {        translatemessage (&msg);//Translate message to generate char message        dispatchmessage (&msg) at the right opportunity;//Transfer message to procedure function    }}

But the problem is that when we drag the dialog box, the interface stops updating.

Therefore, the thread sends the message and the timer effect is relatively good, the interface does not feign animation, when carries on the operation and so on the time does not appear to stop waits the phenomenon.

"Go" MFC Interface Update Implementation method

Related Article

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.