Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Comments and Discussions

QuestionHow to make it work? Pinmembervirdigs18-Aug-09 0:53 
Generalnot working under Vista (2) PinmemberT800G9-Jul-08 7:51 
GeneralRe: not working under Vista (2) Pinmemberwinnxm29-Oct-08 21:02 
GeneralRe: not working under Vista (2) Pinmemberwinnxm29-Oct-08 21:02 
Generalnot working under Vista PinmemberBeiley26-Jan-08 12:28 
Generalthis is not wroking PinmemberK Rajeev29-Nov-07 3:34 
QuestionBalloon at UI [modified] PinmemberLupinTaiwan10-Nov-07 15:57 
GeneralIt got my 1 PinmemberPriyank Bolia18-Apr-07 21:17 
GeneralRe: It got my 1 Pinmembernicoster18-Apr-07 21:34 
GeneralRe: It got my 1 PinmemberPriyank Bolia18-Apr-07 22:46 
NewsGot my 5 PinmemberSudhir Mangla18-Apr-07 18:55 
GeneralRe: Got my 5 Pinmembernicoster18-Apr-07 21:36 
GeneralNice job! PinmemberDavid Howe18-Apr-07 15:57 
QuestionWhat? Pinmemberpartymonkey18-Apr-07 11:27 
AnswerRe: What? Pinmembernicoster18-Apr-07 15:05 
GeneralRe: What? Pinmemberpartymonkey18-Apr-07 16:06 
GeneralRe: What? Pinmembernicoster18-Apr-07 16:39 
GeneralRe: What? Pinmemberfanium22-Sep-08 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 18 Apr 2007
Article Copyright 2007 by nicoster
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid