Click here to Skip to main content
15,886,001 members
Articles / Desktop Programming / MFC
Article

Updating modal dialog contents using callbacks

Rate me:
Please Sign up or sign in to vote.
2.87/5 (7 votes)
7 Dec 1999 95.3K   1.3K   33   9
How to update a modal dialog contents using a callback function
  • Download demo project - 16 Kb
  • This article shows how to update the contents of the modal dialog using a callback functions.

    Some background first. When developing communication applications, it is usually necessary to display a modal dialog box that contains some information about what is happening in the background (statistics, state of the communication etc). Most common solution is to pass all the knowledge about the background process(es) to the modal dialog and then use a timer to periodically update the contents of the dialog controls. This solution is Ok but complicates the design of the dialog and makes it very difficult to have a generic dialog that displays different kind of information depending on the place where it is created. Important side-effect of this solution is either the need to provide access to all data members of the background process classes to the dialog or the need to declare all data members public. Both options complicate the source code.

    The solution presented here uses a callback function given to the dialog via its constructor. Callback function is a member function that belongs to a CWnd derived class (exactly the same technique may be used with a callback function belonging to the CObject derived class). The responsibility of the dialog is to create a timer and periodically (from within OnTimer() handler) call the supplied callback function. Dialog may give some information to the callback function. It will collect dta from the callback function and use it to update the contents of its controls. Contrary to the common solution presented above, only the callback function needs to have access to background process and since it is a callback function it may as well be a part of the background process.

    This design reduces the complexity of the dialog and enables a generic dialog to display different kind of information using different types of callback functions. Another important side-effect is that the application design is simpler and more robust.

    First, we need to create a callback function type:

    typedef BOOL (CWnd::*TWindowUpdate)(int& nCount, CString& sText, DWORD dwData);

    Arguments of this function are application specific. Then we modify the constructor of the dialog:

    // pWindow is a pointer to the CWnd derived object that implements the callback function
    TTestDialog(TWindowUpdate pUpdateCallback, CWnd *pWindow,
                DWORD dwData = 0, CWnd* pParent = NULL)  // standard constructor

    At last, we call the callback function (most often from the OnTimer() handler).

    if (m_pWindow && m_pWindowUpdateCallback) 
    {
      int nCount = 0;
      CString str;
      if ((m_pWindow->*m_pWindowUpdateCallback)(nCount, str, m_dwData)) 
      {
         CString strTemp;
         strTemp.Format("%d", nCount);
         m_ItemCount.SetWindowText(strTemp);
         m_LastItem.SetWindowText(str);
      }
    }

    Callback function is implemented in a CWnd derived window (main frame, view etc).

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer SCA d.o.o.
    Serbia Serbia
    I am a cofounder of SCA Software, company that specializes in software for process control, visualization and communication. Programming for the last 10 years in C++, Delphi. Visual C++ for the last 6 years. Degree in Electronics Engineering and Telecommunications.

    Comments and Discussions

     
    GeneralGreat tutorial Pin
    Anthony_Yio20-Nov-03 16:26
    Anthony_Yio20-Nov-03 16:26 
    GeneralIs there a way to find all the modal dialog boxes open within an application Pin
    Hawkeye10-Sep-02 10:02
    Hawkeye10-Sep-02 10:02 
    GeneralRe: Is there a way to find all the modal dialog boxes open within an application Pin
    Gary R. Wheeler21-Nov-02 13:50
    Gary R. Wheeler21-Nov-02 13:50 
    GeneralBug Pin
    Sancy26-Aug-02 20:08
    Sancy26-Aug-02 20:08 
    GeneralRe: Bug Pin
    Anthony_Yio20-Nov-03 16:23
    Anthony_Yio20-Nov-03 16:23 
    GeneralNice job Pin
    Member 4541521-Aug-02 5:54
    Member 4541521-Aug-02 5:54 
    GeneralWell Done Pin
    24-Sep-01 20:00
    suss24-Sep-01 20:00 
    General[Q] Pin
    24-Jul-01 22:34
    suss24-Jul-01 22:34 
    GeneralCorrection Pin
    Zoran M.Todorovic23-Jan-00 11:30
    sussZoran M.Todorovic23-Jan-00 11:30 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.