Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / MFC

Delay MessageBox with auto-close option

,
Rate me:
Please Sign up or sign in to vote.
4.97/5 (28 votes)
13 Aug 2002Ms-PL7 min read 295.5K   5.4K   73  
This message box delays its dismissal by disabling the OK button for a delay interval. It also has an optional auto-close feature. There are two versions, one uses a WH_CBT hook and some basic window tricks like sub-classing to achieve its goal. The other one is more MFC-ied.
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "#define _AFX_NO_SPLITTER_RESOURCES\r\n"
    "#define _AFX_NO_OLE_RESOURCES\r\n"
    "#define _AFX_NO_TRACKER_RESOURCES\r\n"
    "#define _AFX_NO_PROPERTY_RESOURCES\r\n"
    "\r\n"
    "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
    "LANGUAGE 9, 1\r\n"
    "#pragma code_page(1252)\r\n"
    "#include ""res\\DelayMsgBoxDemo.rc2""  // non-Microsoft Visual C++ edited resources\r\n"
    "#include ""afxres.rc""         // Standard components\r\n"
    "#endif\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Icon
//

// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDR_MAINFRAME           ICON                    "res\\DelayMsgBoxDemo.ico"
ICONEXCLAMATION         ICON                    "res\\icon1.ico"
ICONQUESTION            ICON                    "res\\icon2.ico"
ICONSTOP                ICON                    "res\\icon3.ico"
ICONINFORMATION         ICON                    "res\\icon4.ico"

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DELAYMSGBOXDEMO_DIALOG DIALOGEX 0, 0, 249, 263
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | 
    WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Delay MessageBox Demo - Nish, Shog9"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    EDITTEXT        IDC_EDIT1,16,28,214,40,ES_MULTILINE | ES_AUTOVSCROLL | 
                    ES_WANTRETURN
    EDITTEXT        IDC_EDIT2,83,78,30,12,ES_AUTOHSCROLL
    CONTROL         "Close message box after delay interval",IDC_CHECK1,
                    "Button",BS_AUTOCHECKBOX | WS_TABSTOP,18,99,145,14
    CONTROL         "Show icon",IDC_CHECK2,"Button",BS_AUTOCHECKBOX | 
                    WS_TABSTOP,18,120,96,10
    CONTROL         "Information",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON | 
                    WS_GROUP,54,157,52,14
    CONTROL         "Exclamation",IDC_RADIO3,"Button",BS_AUTORADIOBUTTON,157,
                    157,57,14
    CONTROL         "Stop",IDC_RADIO2,"Button",BS_AUTORADIOBUTTON,54,191,54,
                    14
    CONTROL         "Question mark",IDC_RADIO4,"Button",BS_AUTORADIOBUTTON,
                    157,191,64,14
    PUSHBUTTON      "Show MessageBox",IDC_BUTTON1,149,77,80,18
    PUSHBUTTON      "Exit",IDCANCEL,176,236,50,16
    LTEXT           "Message Text",IDC_STATIC,15,15,55,10
    LTEXT           "Delay in seconds",IDC_STATIC,15,77,55,10
    GROUPBOX        "Choose icon",IDC_STATIC,16,137,214,89
    ICON            ICONINFORMATION,IDC_STATIC,24,154,20,20
    ICON            ICONSTOP,IDC_STATIC,24,188,20,20
    ICON            ICONEXCLAMATION,IDC_STATIC,126,154,20,20
    ICON            ICONQUESTION,IDC_STATIC,126,188,20,20
END


/////////////////////////////////////////////////////////////////////////////
//
// Version
//

VS_VERSION_INFO VERSIONINFO
 FILEVERSION 1,0,0,1
 PRODUCTVERSION 1,0,0,1
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904e4"
        BEGIN
            VALUE "CompanyName", "TODO: <Company name>"
            VALUE "FileDescription", "TODO: <File description>"
            VALUE "FileVersion", "1.0.0.1"
            VALUE "InternalName", "DelayMsgBoxDemo.exe"
            VALUE "LegalCopyright", "TODO: (c) <Company name>.  All rights reserved."
            VALUE "OriginalFilename", "DelayMsgBoxDemo.exe"
            VALUE "ProductName", "TODO: <Product name>"
            VALUE "ProductVersion", "1.0.0.1"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1252
    END
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO 
BEGIN
    IDD_DELAYMSGBOXDEMO_DIALOG, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 242
        TOPMARGIN, 7
        BOTTOMMARGIN, 256
    END
END
#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// RT_MANIFEST
//

IDR_MANIFEST            RT_MANIFEST             "res\\DelayMsgBoxDemo.manifest"
#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
#define _AFX_NO_SPLITTER_RESOURCES
#define _AFX_NO_OLE_RESOURCES
#define _AFX_NO_TRACKER_RESOURCES
#define _AFX_NO_PROPERTY_RESOURCES

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#pragma code_page(1252)
#include "res\DelayMsgBoxDemo.rc2"  // non-Microsoft Visual C++ edited resources
#include "afxres.rc"         // Standard components
#endif

/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Written By
Software Developer
United States United States
Poke...

Comments and Discussions