Environment: VC++ 6.0, VC++ .NET Beta 2, Windows 2000
There are times when you want to make messages pop up in an unobtrusive
way and display messages to the user without interrupting him with dialog
This article shows you the source code about making a pop up message
window like MSN Messenger.
Include the following files in your project.
Following lines show how to create an object of CTaskBarMsgWnd
and use it to pop up a window. We use a pseudo constructor 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 poping 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 and the window’s thread
more responsive than using Sleep()API which was used earlier. Using Sleep() made the thread block till
the window got destroyed.
CTaskBarMsgWnd* t_MsgWnd = CTaskBarMsgWnd::CreateObject(
_T(“Some idiot has signed in !!”),// the message to be displayed
180, // width of the window
150, // height of window
4000, // time in milliseconds for the message to be displayed
10, // delay for animation, how fast the window opens
// and closes in milliseconds
CRect(30, 30, 130, 110), // rectangle in the window where the
// message will be displayed
RGB(120, 0, 0), // Color of the background
RGB(255, 255, 255) // Color of the text
Creation of Window in Separate Thread
The earlier version of this article created the message window in the
same thread. Hence there were some performance hits because the
main window was not much responsive to user inputs like menu
selections, window drag, window resize etc.
In this updated version of article we will create the message
window in a separate thread. Out of the two threads provided by MFC we chose the
user interface thread creation. Create a new class called CPopWndThread derived from CWinThread..Override the InitInstance() of this thread as shown below
m_pMainWnd = CTaskBarMsgWnd::CreateObject(
_T(“Some idiot has signed in !!”),
CRect(30, 30, 130, 110),
RGB(120, 0, 0),
RGB(255, 255, 255)
CTaskBarMsgWnd* pMsgWnd = (CTaskBarMsgWnd*)m_pMainWnd;
The thread and the associated window are destroyed automatically when the message window
collapses in the OnTimer() function of CTaskBarMsgWnd using DestroyWindow() and PostQuitMessage() to exit the thread.
Creation of Thread
The separate user interface thread is created in the CMainFrame::OnMessagePop()
using the following code,
The PopMsg() function first checks where the Task Bar is on the
desktop. There are only four cases
- Task Bar at the bottom of the screen.
- Task Bar at top of the screen.
- Task Bar at the left of the screen.
- Task Bar at the right of the screen.
The follwing code of PopMsg() function shown
if (CheckIfTaskBarBottom()) // Most frequent case is status
// bar at bottom
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
- IDT_POP_WINDOW_TIMER -> Gets triggered for animating
- IDT_SHOW_WINDOW_TIMER -> Gets triggered for showing and
keeping the window in position for some time
- IDT_COLLAPSE_WINDOW_TIMER -> Gets triggered for animating
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
the appropriate animation calculations.
The window is automatically deleted after collapsing in the
OnTimer() function using
That’s the reason we force this window’s creation on heap
The class CTaskBarMsgWnd is derived from CFrameWnd. The title bar is
removed int the follwing code in the
CFontobjects are created, one underlined
font and the other non-underlined. We show the underlined font when mouse
is above the window using
WM_MOUSEHOVER message). Similarly we use the non-underlined font when the
mouse leaves the window with
WM_MOUSELEAVE message). Initialize a “hand” cursor
m_hCursorfor showing the mouse when it is over the window.
OnLButtonDown() for the WM_LBUTTONDOWN message
to suit any thing you would like to do…!!!!. At this point
OnLBottonDown() only shows a message box. You can change and
do anything to suit your needs.
int CTaskBarMsgWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )
if (CFrameWnd::OnCreate(lpCreateStruct) == –1)
ModifyStyle(WS_CAPTION, 0, SWP_FRAMECHANGED); // removes title bar
// Start creating two fonts, one underlined , other non underlined
// LOGFONT structure for font properties
::ZeroMemory (&lf, sizeof (lf));
lf.lfHeight = 100;
lf.lfWeight = FW_BOLD;
lf.lfUnderline = TRUE;
::strcpy (lf.lfFaceName, _T(“Arial”));
// Prepare for an underlined font
// Prepare an non undelined font
lf.lfUnderline = FALSE;
// Initialize the cursor.
m_hCursor = ::LoadCursor(NULL, IDC_HAND);
demo project – 22 Kb
source – 7 Kb