Click here to Skip to main content
13,093,720 members (57,141 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked
Posted 18 Apr 2007

MessageBalloon - an enhanced MessageBox

, 18 Apr 2007
Rate this:
Please Sign up or sign in to vote.
An enhanced MessageBox, no need for user interaction to dismiss

Screenshot - msgballoon.gif


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:

        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:

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

        memset(this, 0, sizeof(*this));
        cbSize = sizeof(*this);
        hWnd = wh.hwnd_;
        hIcon = _hIcon ? _hIcon : 
        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.


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

China China
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow to make it work? Pin
virdigs18-Aug-09 0:53
membervirdigs18-Aug-09 0:53 
Generalnot working under Vista (2) Pin
T800G9-Jul-08 7:51
memberT800G9-Jul-08 7:51 
GeneralRe: not working under Vista (2) Pin
winnxm29-Oct-08 21:02
memberwinnxm29-Oct-08 21:02 
GeneralRe: not working under Vista (2) Pin
winnxm29-Oct-08 21:02
memberwinnxm29-Oct-08 21:02 
Generalnot working under Vista Pin
Beiley26-Jan-08 12:28
memberBeiley26-Jan-08 12:28 
Generalthis is not wroking Pin
K Rajeev29-Nov-07 3:34
memberK Rajeev29-Nov-07 3:34 
QuestionBalloon at UI [modified] Pin
LupinTaiwan10-Nov-07 15:57
memberLupinTaiwan10-Nov-07 15:57 
GeneralIt got my 1 Pin
Priyank Bolia18-Apr-07 21:17
memberPriyank Bolia18-Apr-07 21:17 
GeneralRe: It got my 1 Pin
nicoster18-Apr-07 21:34
membernicoster18-Apr-07 21:34 
GeneralRe: It got my 1 Pin
Priyank Bolia18-Apr-07 22:46
memberPriyank Bolia18-Apr-07 22:46 
NewsGot my 5 Pin
Sudhir Mangla18-Apr-07 18:55
memberSudhir Mangla18-Apr-07 18:55 
GeneralRe: Got my 5 Pin
nicoster18-Apr-07 21:36
membernicoster18-Apr-07 21:36 
GeneralNice job! Pin
David Howe18-Apr-07 15:57
memberDavid Howe18-Apr-07 15:57 
QuestionWhat? Pin
partymonkey18-Apr-07 11:27
memberpartymonkey18-Apr-07 11:27 
AnswerRe: What? Pin
nicoster18-Apr-07 15:05
membernicoster18-Apr-07 15:05 
GeneralRe: What? Pin
partymonkey18-Apr-07 16:06
memberpartymonkey18-Apr-07 16:06 
GeneralRe: What? Pin
nicoster18-Apr-07 16:39
membernicoster18-Apr-07 16:39 
GeneralRe: What? Pin
fanium22-Sep-08 3:50
memberfanium22-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 18 Apr 2007
Article Copyright 2007 by nicoster
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid