Click here to Skip to main content
15,868,419 members
Articles / Desktop Programming / MFC
Article

MessageBalloon - an enhanced MessageBox

Rate me:
Please Sign up or sign in to vote.
3.19/5 (28 votes)
18 Apr 20072 min read 179.4K   3.1K   73   18
An enhanced MessageBox, no need for user interaction to dismiss

Screenshot - msgballoon.gif

Introduction

MessageBox() may be one of the most popular API in Windows. Whenever you want to notify user something, just call it and a messagebox pops up. But MessageBox has some shortcomings. One of which is that when it shows up, the user has to click "OK" to dismiss it. What's worse, if it's in an unattended process, a messagebox interrupts everything.

Maybe that's why Microsoft invented the Notification Balloon. In some circumstances, a notification balloon may be the best choice because the user has been informed. No interaction needed. Processing continues. And, pretty cool, eh?

To use the notification balloon, there is no handy function like messagebox to call; you have to fill the dirty NOTIFYICONDATA struct with care, call the ugly Shell_NotifyIcon() to show it, and then call it again to dismiss it. That's not what our lazy programmer wants. We need something like a MessageBalloon() - just pass in the message and the caption, done. Here it comes!

Using the code

I introduced a class MessageBalloon to do the whole thing. you can use it like this:

C++
//...
    {
        MessageBalloon mb("A long time operation..", "MsgBalloon Demo");
        Sleep(3000);    // simu a long time operation.
    }
    //...

Because balloons would be destroyed in a destructor, the surrounding braces are necessary to trigger the destruction of the MessageBalloon object after the long time operation finishes.

Pretty easy to use. right?

Points of Interest

The implementation is easy too. Here is everything underneath:

C#
struct MessageBalloon : public NOTIFYICONDATA
{
    MessageBalloon(LPCTSTR _szInfo = _T("Prompt"), LPCTSTR _szInfoTitle = 0, 
        LPCTSTR _szTip = 0, HICON _hIcon = 0)
    {
        static struct wnd_holder{
            HWND hwnd_;
            wnd_holder(){hwnd_ = CreateWindowW(L"STATIC", 0, 0, 0, 0, 0, 0, 0, 
                0, 0, 0);}
            ~wnd_holder(){DestroyWindow(hwnd_);}
        } wh;

        memset(this, 0, sizeof(*this));
        cbSize = sizeof(*this);
        hWnd = wh.hwnd_;
        hIcon = _hIcon ? _hIcon : 
           ::LoadIcon(::GetModuleHandle(NULL),
           MAKEINTRESOURCE(128/*IDR_MAINFRAME*/));
        if (! hIcon) hIcon = ::LoadIcon(NULL, IDI_INFORMATION);
        uFlags = NIF_INFO | NIF_TIP | NIF_ICON; 
        dwInfoFlags = NIIF_INFO;
        _tcscpy(szInfo, _szInfo);
        if (! _szInfoTitle) LoadString(::GetModuleHandle(NULL), 
            57344/*AFX_IDS_APP_TITLE*/, szInfoTitle, sizeof(szInfoTitle));
        else _tcscpy(szInfoTitle, _szInfoTitle);
        _tcscpy(szTip, _szTip ? _szTip : szInfoTitle);

        Shell_NotifyIcon(NIM_ADD, this);
    }

    ~MessageBalloon(){Shell_NotifyIcon(NIM_DELETE, this);}
};

Nothing obscure. To obtain the window handle, a window has been created which is held by a static variable. This enables us to use MessageBalloon several times while only creating/destroying the window once. If the class is being used in MFC, the IDR_MAINFRAME icon would be loaded as a notification icon, or else system default information icon would be used. The default balloon title is AFX_IDS_APP_TITLE in MFC.

The notification balloon has been only supported in Windows 2000 or above, so the MessageBalloon can only be used in those platforms. To compile the source code, Windows 2000 SDK or above should be installed.

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
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to make it work? Pin
gs_virdi18-Aug-09 0:53
gs_virdi18-Aug-09 0:53 
Generalnot working under Vista (2) Pin
T800G9-Jul-08 7:51
T800G9-Jul-08 7:51 
GeneralRe: not working under Vista (2) Pin
winnxm29-Oct-08 21:02
winnxm29-Oct-08 21:02 
GeneralRe: not working under Vista (2) Pin
winnxm29-Oct-08 21:02
winnxm29-Oct-08 21:02 
Generalnot working under Vista Pin
Beiley26-Jan-08 12:28
Beiley26-Jan-08 12:28 
Generalthis is not wroking Pin
K Rajeev29-Nov-07 3:34
K Rajeev29-Nov-07 3:34 
QuestionBalloon at UI [modified] Pin
LupinTaiwan10-Nov-07 15:57
LupinTaiwan10-Nov-07 15:57 
GeneralIt got my 1 Pin
Priyank Bolia18-Apr-07 21:17
Priyank Bolia18-Apr-07 21:17 
GeneralRe: It got my 1 Pin
nicoster18-Apr-07 21:34
nicoster18-Apr-07 21:34 
GeneralRe: It got my 1 Pin
Priyank Bolia18-Apr-07 22:46
Priyank Bolia18-Apr-07 22:46 
NewsGot my 5 Pin
Sudhir Mangla18-Apr-07 18:55
professionalSudhir Mangla18-Apr-07 18:55 
GeneralRe: Got my 5 Pin
nicoster18-Apr-07 21:36
nicoster18-Apr-07 21:36 
I'm glad that you like it.
thanks Big Grin | :-D
GeneralNice job! Pin
David Howe18-Apr-07 15:57
David Howe18-Apr-07 15:57 
QuestionWhat? Pin
partymonkey18-Apr-07 11:27
partymonkey18-Apr-07 11:27 
AnswerRe: What? Pin
nicoster18-Apr-07 15:05
nicoster18-Apr-07 15:05 
GeneralRe: What? Pin
partymonkey18-Apr-07 16:06
partymonkey18-Apr-07 16:06 
GeneralRe: What? Pin
nicoster18-Apr-07 16:39
nicoster18-Apr-07 16:39 
GeneralRe: What? Pin
fanium22-Sep-08 3:50
fanium22-Sep-08 3:50 

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.