Click here to Skip to main content
13,252,362 members (64,305 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 7 Dec 1999

Updating modal dialog contents using callbacks

, 7 Dec 1999
Rate this:
Please Sign up or sign in to vote.
How to update a modal dialog contents using a callback function
  • Download demo project - 16 Kb
  • <!-- Article Starts -->

    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);

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


    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


    About the Author

    Zoran M. Todorovic
    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.

    You may also be interested in...


    Comments and Discussions

    GeneralGreat tutorial Pin
    Anthony_Yio20-Nov-03 17:26
    memberAnthony_Yio20-Nov-03 17:26 
    GeneralIs there a way to find all the modal dialog boxes open within an application Pin
    Hawkeye10-Sep-02 11:02
    memberHawkeye10-Sep-02 11:02 
    GeneralRe: Is there a way to find all the modal dialog boxes open within an application Pin
    Gary R. Wheeler21-Nov-02 14:50
    memberGary R. Wheeler21-Nov-02 14:50 
    GeneralBug Pin
    Sancy26-Aug-02 21:08
    memberSancy26-Aug-02 21:08 
    GeneralRe: Bug Pin
    Anthony_Yio20-Nov-03 17:23
    memberAnthony_Yio20-Nov-03 17:23 
    GeneralNice job Pin
    Third21-Aug-02 6:54
    sussThird21-Aug-02 6:54 
    GeneralWell Done Pin
    hamid24-Sep-01 21:00
    memberhamid24-Sep-01 21:00 
    General[Q] Pin
    linugee24-Jul-01 23:34
    memberlinugee24-Jul-01 23:34 
    GeneralCorrection Pin
    Zoran M.Todorovic23-Jan-00 12:30
    sussZoran M.Todorovic23-Jan-00 12: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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.171114.1 | Last Updated 8 Dec 1999
    Article Copyright 1999 by Zoran M. Todorovic
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid