Click here to Skip to main content
Click here to Skip to main content

MSN Messenger type status bar popup message box

By , 1 Apr 2012
 

Sample Image - StatusBarMsgWnd.jpg

Introduction

One of the interesting things I found in the recent times about Windows programming was the popping up of status bar message windows when somebody logs in MSN messenger. Here is the code I would like to share with anyone out there who would also like to use it, but was not able to do so because of no direct API help for the following task.

How to use the source code

Include the following files in your project.

  • StatusBarMsgWnd.h
  • StatusBarMsgWnd.cpp

Following lines show how to create an object of CStatusBarMsgWnd and use it to pop up a window. We use a pseudo contructor called CreateObject() because we want to force heap creation. For making the normal constructor unavailable we make it "private". We want to make the object on the heap because the PopMsg() function will trigger timers and animate the popping up and collapsing of the window even after it has returned. So the message window should remain in memory till all timers subside and the window collapses. This even makes the parent application more responsive than using Sleep() API which I had used earlier. Using Sleep() made the application block till the window got destroyed.

CStatusBarMsgWnd* t_MsgWnd = CStatusBarMsgWnd::CreateObject(
    _T("Some idiot has signed in !!"), // the message to be displayed
    180, // width of the window
    150, // height of window   
    4000, // time for the message to be displayed
    10, // delay for animation, how fast the window opens and closes
    CRect(30, 30, 130, 110), // rectangle in the window where the 
                             //message will be displayed 
    this // parent of the message window
    );
t_MsgWndMsg.PopMsg();

Details

The PopMsg() function first checks, where is the status bar on the desktop? There are only four cases

  1. Status bar at the bottom of the screen.
  2. Status bar at top of the screen.
  3. Status bar at the left of the screen
  4. Status bar at the right of the screen.

The following code of PopMsg() function is shown

void CStatusBarMsgWnd::PopMsg()
{
    if (CheckIfStatusBarBottom())  
        // Most frequent case is status bar at bottom
    {
        PopWndForBottomStatusBar();
    }
    else
    {
        if (CheckIfStatusBarTop())
        {
            PopWndForTopStatusBar();
        }
        else
        {
            if (CheckIfStatusBarLeft())
            {
                PopWndForLeftStatusBar();
            }
            else
            {
                PopWndForRightStatusBar();
            }
        }
    }
}

The CheckIfStatusBarBottom() (or CheckIfStatusBarTop(), CheckIfStatusBarLeft()) functions use GetSystemMetrics() and SystemParatmeterInfo() APIs to calculate the full screen area and the area on the screen minus the status bar. Then with some elementary high school mathematics we calculate where exactly the status bar is (bottom, top, left or right) and appropriately show the message window with some animation.

The real action occurs in the OnTimer() function which gets triggered because of WM_TIMER messages. There are three timers

  1. IDT_POP_WINDOW_TIMER -> Gets triggered for animating popup
  2. IDT_SHOW_WINDOW_TIMER -> Gets triggered for showing and keeping the window in position for some time
  3. IDT_COLLAPSE_WINDOW_TIMER -> Gets triggered for animating window collapse

There are three other constants namely STP_BOTTOM, STP_TOP, STP_RIGHT and STP_LEFT which represent where the status bar position is. These are used in OnTimer) for the appropriate animation calculations.

The window is automatically deleted after collapsing in the OnTimer() function using delete this. That's the reason we force this window's creation on heap

Class Details

The class CStatusBarMsgWnd is derived from CFrameWnd. The title bar is removed in the OnCreate() member function. Two CFont objects are created, one underlined font and the other non-underlined. We show the underlined font when mouse is above the window using OnMouseHover() function (for WM_MOUSEHOVER message). Similarly we use the non-underlined font when the mouse leaves the window with OnMouseLeave() function (for WM_MOUSELEAVE message). Initialize a "hand" cursor m_hCursor for showing the mouse when it is over the window.

Rewrite the OnLButtonDown() for the WM_LBUTTONDOWN message to suit any thing you would like to do. At this point OnLButtonDown() only shows a message box. You can change and do anything to suit your needs.

int CStatusBarMsgWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    ModifyStyle(WS_CAPTION, 0, 
        SWP_FRAMECHANGED); // removes title bar

    // Start creating two fonts, one underlined , 
    // other non underlined 
    
    // LOGFONT structure for font properties
    LOGFONT lf;
    ::ZeroMemory (&lf, sizeof (lf));
    lf.lfHeight = 100;
    lf.lfWeight = FW_BOLD;
    lf.lfUnderline = TRUE;

    ::strcpy (lf.lfFaceName, _T("Arial"));

    // Prepare for an underlined font
    m_fontMessageUnderline.CreatePointFontIndirect(&lf);

    // Prepare  an non underlined font
    lf.lfUnderline = FALSE;
    m_fontMessageNoUnderline.CreatePointFontIndirect(&lf);

    // Initialize the cursor.
    m_hCursor = ::LoadCursor(NULL, IDC_HAND);

    return 0;
}

How to use the demo project exe

Double click the popwnd.exe and use the "Message Menu" to pop up a message window

Future Work to be done, in progress...

  • MSN messenger uses color scheme in the message box taking the color from the active title bar.

License

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

About the Author

Prateek Kaul
Web Developer
India India
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhow to change the background?memberhappy888819-Jun-03 19:05 
GeneralImplementing this on a webpage !help!memberrtrajan9-Jun-03 6:28 
GeneralDialog Based Applicationmemberiffi99229-May-03 4:04 
HI chris
I want to use this class with a dialog based application
I have tried to create its instance in Initinstance but it crashed then I tried to create it in InitInstance but it gives an assertion I ignored it and the Icon appeared in the System trayBlush | :O , can you please guide me so that I may easily use it in my application.
 
Regards
Muhammad Aftab Alam
QuestionMicrosoft Web Browser Object into DialogBox, without MFC?memberAdrian Bacaianu8-Apr-03 20:42 
AnswerRe: Microsoft Web Browser Object into DialogBox, without MFC?member.S.Rod.8-Apr-03 21:11 
GeneralTaskbar positionmemberWeng Wei8-Feb-03 0:54 
QuestionHow i stop to transfer the focus on the popup window?memberSaeed A. Siddiqui5-Nov-02 20:21 
AnswerRe: How i stop to transfer the focus on the popup window?sussPrateek K6-Nov-02 4:37 
GeneralRe: How i stop to transfer the focus on the popup window?membersanpee21-Jul-04 16:27 
GeneralRe: How i stop to transfer the focus on the popup window?memberPrateek Kaul21-Jul-04 19:28 
GeneralGDI leaksussAndreyZ18-Sep-02 5:25 
GeneralThankssussRob A.11-Sep-02 10:05 
Generalwill this popup during fullscreen directx gamessussAnonymous14-Aug-02 10:09 
GeneralSublclassing MSN PopupumemberAnonymous26-Apr-02 3:46 
GeneralSome fix for flicker free.memberYou-shin Won29-Jan-02 21:00 
GeneralRe: Some fix for flicker free.memberIgor Green31-Jan-02 4:01 
GeneralStatus BarmemberChoppa12-Nov-01 3:42 
GeneralRe: Status BarmemberPrateek12-Nov-01 21:00 
GeneralClose while pop-upmemberAnonymous2-Nov-01 6:25 
GeneralRe: Close while pop-upmemberYou-shin Won29-Jan-02 20:52 
QuestionCan I do it in .NetmemberAnonymous28-Oct-01 23:58 
QuestionIs there any message length restriction?memberPhips Xue28-Oct-01 17:12 
AnswerRe: Is there any message length restriction?memberPrateek Kaul29-Oct-01 2:55 
GeneralBackground colormemberHenk Devos22-Oct-01 0:17 
GeneralRe: Background colormemberPrateek Kaul22-Oct-01 3:00 
QuestionWhy copyright sh***y classes like this ?memberAnonymous19-Oct-01 7:26 
AnswerRe: Why copyright sh***y classes like this ?memberChristian Skovdal Andersen21-Oct-01 16:54 
AnswerRe: Why copyright sh***y classes like this ?memberCrispy29-Oct-01 11:47 
AnswerRe: Why copyright sh***y classes like this ?memberOJ7-Jan-03 19:19 
Questionnice .. but..?memberwasp18-Oct-01 2:33 
AnswerRe: nice .. but..?memberPrateek Kaul18-Oct-01 3:24 
Generalerrormemberwasp18-Oct-01 3:41 
GeneralRe: errormemberPrateek Kaul18-Oct-01 4:12 
GeneralRe: errormemberwasp18-Oct-01 4:29 
Generalmaybe the reason is:memberwasp18-Oct-01 4:47 
Generalfinished!memberwasp18-Oct-01 23:05 
GeneralRe: errormembercristitomi21-Mar-07 5:29 
GeneralRe: nice .. but..?memberhyun wook Lim22-Jan-02 5:54 
GeneralRe: nice .. but..?memberAnonymous15-Mar-02 6:40 
GeneralError appears while buildingmemberImran Farooqui17-Oct-01 7:34 
GeneralRe: Error appears while buildingmembertradesniper17-Oct-01 18:53 
GeneralRe: Error appears while buildingmemberPrateek Kaul17-Oct-01 20:36 
GeneralExcellent !!memberRashid Thadha15-Oct-01 22:45 
GeneralClicking on windowmemberBrigham W. Thorp15-Oct-01 7:34 
GeneralRe: Clicking on windowmemberPrateek Kaul15-Oct-01 7:41 
GeneralRe: Clicking on windowmemberBrigham W. Thorp15-Oct-01 8:10 
GeneralRe: Clicking on windowmemberOnkar Singh17-Oct-01 21:09 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 1 Apr 2012
Article Copyright 2001 by Prateek Kaul
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid