Click here to Skip to main content
15,860,844 members
Articles / Desktop Programming / MFC

Message Balloons

,
Rate me:
Please Sign up or sign in to vote.
4.84/5 (11 votes)
1 Apr 2012CPOL4 min read 237.2K   7.2K   97   27
Message Balloons as opposed to Message Boxes

See recent updates to the source, or download older versions of this source below:

Sample Image - MessageBalloons.jpg

Introduction

There are times when one would like to display messages in an non-obtrusive way. Message Balloons are one of the new ways. Message Balloons are taking precedence over message boxes for small display messages as they even prevent the user from clicking an “OK” and still get the message across. These kind of Message Balloons are becoming a part of standard user interface with MSN explorer and Windows XP. Still no library has been provided for making these balloons. Hence this attempt.

How to use the class provided ?

Include the following two files in your project:

  • BalloonTip.h
  • BalloonTip.cpp

There are two interfaces to create and show the message balloons.

  • CBalloonTip::Show()
  • CBalloonTip::Hide()

CBalloonTip::Show()-:

This is a pseudo constructor because we want to force heap creation of the balloon, so that it remains in the memory even after it has been called. It will be automatically destroyed when a it receives a WM_TIMER message which is set in the CBalloon::MakeVisible(). The other method to destroy the Balloon before the timer ticks is to call the static function CBalloon::Hide(). The constructor for CBalloonTip is protected, hence this helps in forcing heap creation. The following code shows how to create a Message Balloon and show it.

LOGFONT lf;
::ZeroMemory (&lf, sizeof (lf));
lf.lfHeight = 16;
lf.lfWeight = FW_BOLD;
lf.lfUnderline = FALSE;
::strcpy (lf.lfFaceName, _T("Arial"));
        
CBalloonTip::Show(
    CPoint(200, 200),         // Point on the screen where the tip will be shown
    CSize(250, 100),          // Size of the total rectangle encompassing the balloon 
    _T("Please enter a password !!"), // Message to be shown in the balloon
    lf,                               // LOGFONT structure for font properties 
    5,                 // Time in seconds to show the balloon
    FALSE              // TRUE  == Balloon is up(Balloon Tip is down) 
                       // FALSE ==  Balloon is down(Balloon Tip is up)
);

See the updates section to see more about this function.

CBalloonTip::Hide()-:

This is for destroying the Balloon for any particular condition that may arise before it is automatically destroyed in the CBalloonTip::OnTimer(). Either way if CBalloonTip::Hide() is not called the Balloon is destroyed in CBalloonTip::OnTimer(). So the caller (user of this class) does not need to worry about the memory cleanup.

See the updates section to see more about this function.

Class Design Details

CBalloonTip is derived from CFrameWnd. The balloon is created from the combination of two regions, one is a polygon region to show the the Balloon Tip and the other is a Round Rectangle region. This is done in the CBalloonTip::OnCreate().

int CBalloonTip::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    //..........Some code
    //...................

    CRgn rgnComb;	
    
    rgnComb.CreateRectRgn(t_Rect.left, t_Rect.top, t_Rect.right, t_Rect.bottom);
		
    int iRetComb = rgnComb.CombineRgn(&m_rgnTip, &m_rgnRoundRect, RGN_OR);
}

The m_rgnTip and m_rgnRoundRect are created with different calculations depending upon whether the balloon tip is down or up. The visible window or balloon region is finally set using SetWindowRgn (rgnComb.operator HRGN(), TRUE) in the CBalloonTip::OnCreate().

The Balloon window is not shown up in the taskbar by making the Balloon a child of an invisible parent in CBalloonTip::PreCreateWindow() This parent window is a member variable (CBalloon::m_wndInvisibleParent) of the class itself and is destroyed automatically when the Balloon is destroyed. So the non-appearance of the Balloon window in the task bar is taken care of automatically.

BOOL CBalloonTip::PreCreateWindow(CREATESTRUCT& cs)
{
    //...............Some code
    //........................

    if (!::IsWindow(m_wndInvisibleParent.m_hWnd))
    {
        // Try creating the invisible parent window
        PCSTR pstrOwnerClass = ::AfxRegisterWndClass(0);
       
        BOOL bError = m_wndInvisibleParent.CreateEx(
                          0,
                          pstrOwnerClass, 
                          _T(""), 
                          WS_POPUP,
	                  0,
                          0,
                          0,
                          0,
		          NULL,
                          0
                      );
    }
}

Window Creation

The balloon is created with respect from the point where it is supposed to be displayed. So the calculation of balloon rectangle has to be recalculated w.r.t. the point at which it is to be shown. Different calculations have to be done if the Balloon tip is down or up. This is done in CBalloontip::Show() as shown below.

CBalloonTip* CBalloonTip::Show(CPoint pt, CSize size, CString strMessage, 
        LOGFONT lf, UINT nSecs, BOOL bBalloonUp)
{
    //.........Some code
    //..................

    if (bBalloonUp)
    {
        nRectLeft   = pt.x - (size.cx * 0.65);
        nRectRight  = pt.x + (size.cx * 0.35);
        nRectTop    = pt.y - size.cy;
        nRectBottom = pt.y;
    }
    else
    {
        nRectLeft   = pt.x - (size.cx * 0.35);
        nRectRight  = pt.x + (size.cx * 0.65);
        nRectTop    = pt.y;
        nRectBottom = pt.y + (size.cy);
    }
}

The actual Windows® window balloon is created in CBalloonTip::Show() using

pBalloonTip->Create(CRect(nRectLeft, nRectTop, nRectRight, nRectBottom));

The CRect passed in the above function will represent the actual position of the balloon in screen co-ordinates. The timer for the Balloon destruction is set in the next line in CBalloonTip::Show()

pBalloonTip->MakeVisisble(nSecs);
  • Only one instance of the Balloon is allowed to created at a time using the static variable CBalloonTip::nBalloonInstances

Overview of the demo project

Demo

Just try clicking the "OK" button without entering anything in the the edit boxes.

Compatibility

Tested using VC++ 6 with MFC on Windows 2000. Should work fine on other Windows OSs(95/98/NT/ME/XP) also, as it uses no OS specific code.

Updates

The following changes to the code in the updated version have been made by R. Khalili:

  1. The OnPaint and Show methods have been updated to do a better job.
  2. The message balloon doesn't stay on top of applications as you switch between apps.
  3. There is more rigorous checking to prevent crashes as a result of null pointers, etc...

Conclusion

I hope that this contribution will make some improvement in the user interface code of your forthcoming programs!!! Any suggestions, improvements or bugs detected are welcome. Enjoy...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Web Developer
Canada Canada
I have been programming for the past 5 years or so. I started out with C for about 1 year, then took a break and ended up doing Access and VB for a short while. It was then followed by more C, a bit of Java and database development in Oracle and SQL Server and web development in ASP, until I arrived in the C++ and MS windows world. I have been working with MFC for the past 2-3 years and have some COM and Win32 API experience as well. I have recently begun the journey of .NET as well, trying to keep up with the latest hype.

Comments and Discussions

 
GeneralMy vote of 4 Pin
magicpapacy8-Apr-12 21:30
magicpapacy8-Apr-12 21:30 
GeneralIt MUST get the bug:Memory Leak Pin
Shuly16-May-07 0:43
Shuly16-May-07 0:43 
GeneralSome remarks Pin
cristitomi21-Mar-07 6:18
cristitomi21-Mar-07 6:18 
QuestionConvert the code to dll Pin
x137293885-Jan-07 17:24
x137293885-Jan-07 17:24 
GeneralIt don't works! Pin
markechidna30-Oct-04 4:57
markechidna30-Oct-04 4:57 
GeneralRe: It don't works! Pin
ThatsAlok9-Jan-05 22:19
ThatsAlok9-Jan-05 22:19 
GeneralFont Color Change Pin
diilbert30-Apr-04 10:26
diilbert30-Apr-04 10:26 
GeneralRe: Font Color Change Pin
André Voigt5-May-04 1:01
André Voigt5-May-04 1:01 
GeneralERROR and 32 Warnings Pin
Balkrishna Talele11-Dec-03 23:39
Balkrishna Talele11-Dec-03 23:39 
GeneralRe: ERROR and 32 Warnings Pin
diilbert30-Apr-04 15:30
diilbert30-Apr-04 15:30 
GeneralRe: ERROR and 32 Warnings Pin
sonawane ajay2-Aug-04 19:44
sonawane ajay2-Aug-04 19:44 
Generalplayer's fix Pin
p1ayer24-Nov-03 20:45
p1ayer24-Nov-03 20:45 
GeneralRe: player's fix Pin
cristitomi21-Mar-07 6:01
cristitomi21-Mar-07 6:01 
Generalnew source bad for 98 Pin
EndLESSracingZ22-Sep-03 4:30
EndLESSracingZ22-Sep-03 4:30 
GeneralCreateBalloon Suggestion... Pin
Member 39546424-May-03 13:38
Member 39546424-May-03 13:38 
// add one parameter to customize the MESSAGE

void CMessageBalloonDlg::CreateBalloon(int nItemID, CString szMesg)
{
CRect rect;
GetDlgItem(nItemID)->GetWindowRect(&rect);
//DisplayBalloon(&rect, "Please enter a value or cancel this dialog.");
DisplayBalloon(&rect, szMesg);
}

note: please dont 4get to add in ur "*.h" file

thanks!

go for it!
QuestionMicrosoft Web Browser Object into DialogBox, without MFC? Pin
Adrian Bacaianu8-Apr-03 20:46
Adrian Bacaianu8-Apr-03 20:46 
GeneralNot Working + Fix Pin
Bandu Patil7-Jun-02 0:17
Bandu Patil7-Jun-02 0:17 
GeneralRe: Not Working + Fix Pin
baifriend3-Dec-02 14:51
baifriend3-Dec-02 14:51 
GeneralRe: Not Working + Fix Pin
Anonymous3-Dec-02 18:15
Anonymous3-Dec-02 18:15 
GeneralMemory leak Pin
laotong25-Mar-02 23:07
laotong25-Mar-02 23:07 
QuestionNot working under Win2000? Pin
Davide Calabro10-Dec-01 23:18
Davide Calabro10-Dec-01 23:18 
AnswerRe: Not working under Win2000? Pin
Prateek Kaul10-Dec-01 23:47
Prateek Kaul10-Dec-01 23:47 
GeneralRe: Not working under Win2000? Pin
11-Dec-01 19:28
suss11-Dec-01 19:28 
GeneralMessage is non-modal Pin
567890123410-Dec-01 4:35
567890123410-Dec-01 4:35 
GeneralRe: Message is non-modal Pin
Roger Allen11-Dec-01 0:04
Roger Allen11-Dec-01 0:04 

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.