Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   MessageBoxTimeout (http://www.go4expert.com/articles/messageboxtimeout-t3417/)

Sanskruti 15Mar2007 19:44

Before reading this article you may know what is Microsoft foundation classes and Visual C++.I am listing here few of its features.

When MFC was introduced, Microsoft extended the C++ syntax with a series of macros for management of Windows messages, exceptions, run time type identification, and dynamic class instantiation. The macros for Windows messages were intended to reduce memory required by avoiding gratuitous vtable use and provide a more concrete structure for various Visual C++-supplied tools to edit and manipulate code without parsing the full language.

The message-handling macros replaced the virtual function mechanism provided by C++. Because some versions of the macros defeated the type checking done by the compiler, their use has been a potential source of bugs for users of MFC.The macros which implemented serialization, exception support, and dynamic runtime types were less problematic, and predated availability of standards-based language extensions by a number of years.Provides an object-oriented programming model to the Windows APIs .

C++ wrapper types for many common Windows resource-related data types that provide automatic closure of handles when the objects creating them go out of scope. Provides a Document/View framework for creating Model-View-Controller-based architectures. Provides utility classes such as CString and collection classes, which are usable even by console applications and many more features are there.

We will see in the following code how a message box which is timed works.

Code: CPP

static BOOL s_fTimedOut;
static HWND s_hwndMBOwnerEnable;

MsgBoxTooLateProc(HWND hWnd, UINT uiMsg, UINT_PTR idEvent, DWORD dwTime)
    s_fTimedOut = TRUE;
    if (s_hwndMBOwnerEnable) EnableWindow(s_hwndMBOwnerEnable, TRUE);

int TimedMessageBox(HWND hwndOwner, LPCTSTR ptszText,
    LPCTSTR ptszCaption, UINT uType, DWORD dwTimeout)
    s_fTimedOut = FALSE;
    s_hwndMBOwnerEnable = NULL;
    if (hwndOwner && IsWindowEnabled(hwndOwner)) {
      s_hwndMBOwnerEnable = hwndOwner;
    UINT idTimer = SetTimer(NULL, 0, dwTimeout, CheapMsgBoxTooLateProc);
    int iResult = MessageBox(hwndOwner, ptszText, ptszCaption, uType);
    if (idTimer) KillTimer(NULL, idTimer);
    if (s_fTimedOut) {     
    MSG msg;
    Message(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
    iResult = -1;
    return iResult;

This TimedMessageBox function acts just like the MessageBox function, except that if the user doesn't respond within dwTimeout milliseconds, we return -1. The limitation is that only one timed message box can be active at a time. If your program is single-threaded, this is not a serious limitation, but if your program is multi-threaded, this will be a problem.

Let us see how it works

The global static variable s_fTimedOut tells us whether we generated a fake WM_QUIT message as a result of a timeout. When the MessageBox function returns, and we indeed timed out, we use the Message function to remove the fake WM_QUIT message from the queue before returning.

Note that we remove the WM_QUIT message only if we were the ones who generated it. In this way, WM_QUIT messages generated by other parts of the program remain in the queue for processing by the main message loop. when we decide that the timeout has occurred, we re-enable the original owner window before we cause the message box to bail out of its message loop by posting a quit message.

parvez.yu 6Mar2008 14:58

Re: MessageBoxTimeout
i am trying to understand

All times are GMT +5.5. The time now is 13:37.