Click here to Skip to main content
15,905,316 members
Articles / Desktop Programming / MFC

Auto-close message box

Rate me:
Please Sign up or sign in to vote.
4.47/5 (11 votes)
23 Jul 2007CPOL1 min read 174.4K   7.4K   31   11
A simple solution for automatically closing a message box.

Screenshot - Image1.jpg

Screenshot - Image2.jpg


This article describes a simple solution for automatically closing a message box. I have looked at many similar articles in the Inetrnet which are quite complex, so I thought I would make this one a little more simple.

Understanding the CMsgBox class

CMsgBox is a class which implements auto-close functionality. This class is derived from the CWnd class. It exposes a method called "MessageBox()", which in turn would call CWnd::MessageBox() to display a message box.

Getting the auto-close functionality implemented is very simple. In the CMsgBox::MessageBox() method, we start a timer (SetTimer() method) just before we call the CWnd::MessageBox API. In the OnTimer method, we try to find the message box window by using its window name (caption). Once found, we post a WM_CLOSE message to close the message box. That's it !

void CMsgBox::MessageBox(CString sMsg, CString sCaption, UINT nSleep, 
                         UINT nFlags, bool bAutoClose)
    // Save the caption, for finding this 
    // message box window later

    m_Caption = sCaption;  

    // If auto close then, start the timer.

        SetTimer(100, nSleep, NULL);

    // Show the message box

    CWnd::MessageBox(sMsg, sCaption, nFlags);

void CMsgBox::OnTimer(UINT nIDEvent) 
    // TODO: Add your message handler code here and/or call default

    BOOL bRetVal = false;

    // Find the message box window using the caption

    CWnd* pWnd = FindWindow(NULL, m_Caption);
    if(pWnd != NULL)
        // Send close command to the message box window

        ::PostMessage(pWnd->m_hWnd, WM_CLOSE, 0, 0);

    // Kill the timer



Using the code

Add these two files to your project: "MsgBox.cpp" and "MsgBox.h". #include "MsgBox.h" wherever appropriate. Create the CMsgBox object as:

CMsgBox obj(this);

Or like this:

CMsgBox obj;

Use the MessageBox() method for displaying the message box. Set the bAutoClose parameter to false if you don't need the auto-close functionality.

obj.MessageBox("This message box will auto close in 2 seconds.", 
               "Auto Close Msg Box", 2000, MB_OK | MB_ICONINFORMATION);


Wasn't that simple? Also, this being my first post, please forgive me for any mistakes.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
India India
Sandeep completed his masters in Computer Applications in 2003 and has been hooked onto programming since then. His first project involved communicating with hardware devices which actually got him more interested in device programming.

Comments and Discussions

GeneralMy vote of 5 Pin
Member 103079991-Sep-14 22:58
professionalMember 103079991-Sep-14 22:58 
QuestionStatic AutoClose MessageBox Pin
Clive Haskins7-Aug-12 2:08
Clive Haskins7-Aug-12 2:08 
GeneralNice work! Pin
Daniel Vlasceanu1-Aug-07 0:20
Daniel Vlasceanu1-Aug-07 0:20 
GeneralRe: Nice work! Pin
melwyn5-Aug-07 9:41
melwyn5-Aug-07 9:41 
GeneralMy implementaion Pin
melwyn30-Jul-07 8:43
melwyn30-Jul-07 8:43 
GeneralRe: My implementaion Pin
ryltsov5-Aug-07 6:54
ryltsov5-Aug-07 6:54 
GeneralFeature request Pin
Ravi Bhavnani24-Jul-07 2:24
professionalRavi Bhavnani24-Jul-07 2:24 
GeneralRe: Feature request Pin
thammadi24-Jul-07 19:54
thammadi24-Jul-07 19:54 
GeneralNice! Pin
Ravi Bhavnani24-Jul-07 2:19
professionalRavi Bhavnani24-Jul-07 2:19 
GeneralLooks useful! Pin
Hans Dietrich23-Jul-07 23:05
mentorHans Dietrich23-Jul-07 23:05 
GeneralRe: Looks useful! Pin
thammadi24-Jul-07 0:39
thammadi24-Jul-07 0:39 

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.