Visual Basic Projects

TransparentBlt: Simulating Microsoft's 'Windows Messenger' Notifications
Step 1: Introduction and Layout
     
Posted:   Wednesday August 14, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   Pierre Alexis, VBnet - Randy Birch
     
 Other project pages:   Step 1: Introduction and Layout
Step 2: Building the Calling Form
Step 3: Building the Notification Form
     
 Prerequisites
None.

From the VBnet mailbag:  A couple of months ago I received an email from a frequent site and newsgroup visitor indicating he had developed an application that displayed a Microsoft Windows Messenger-style notification window (msim). The functionality was pretty cool, and so the code to create the demo app is republished here with the author's permission, and with a few enhancements over the original code.

For those unfamiliar with the msim notification window, when msim wants to alert that perhaps new email or a contact has come on-line, (on a system with the taskbar at the screen bottom) a notification sound is played and a small rectangular window slides up from behind the systray with the announcement. In some cases the clicking the message invokes an action, and in all cases, when the message had displayed for a few seconds, it slides back behind the systray out of view. (I use the taskbar in the default bottom position so have no idea whether msim notifications always appear at the bottom-right of the screen, or whether they always appear at the systray position).

To be 'different', this demo does not position the notification at the lower right corner but rather appears from the screen top, either right-aligned or left-aligned to the edge of the screen through a customizable setting in the project. But because the notification window calculates its display position after acquiring the work area of the screen, as coded the notification window not be obscured by the taskbar if you position it at the top of the window. And unlike the real msim notification window that appears to slide into view, this version unrolls / rolls to reveal and hide itself. 

The majority of the work takes place in the small notification form - the larger form shown just grabs the notification sound to use from the registry and calls the notification form's ShowMessage routine. The four illustrations above show:

  • ready to go
  • starting to reveal itself
  • fully revealed, with gradient background and icon (the gradient appears coarse in the illustration - a result of converting the images to gif).
  • hyperlink-like highlighting when the mouse moves over the form.

Using a standard label the message displayed is set at runtime and can contain any text, although its length is naturally limited by the size of the notification window. But the notification form can have a gradient background of any colour as well display a bitmap (complete with transparency) or an icon of your choosing. A copy of the msim bitmap is provided below.

Here are the key code execution steps of the notification window:

  • notification window's ShowMessage routine is called from the source form
  • frmNotify's Form_Initialize routine is invoked, which sizes and positions the label as well as set some properties used by the form
  • the form is cleared, and the gradient background is drawn
  • if an image is specified, it's blt'd right onto the form. If the image was a bitmap (see below) a transparent colour can also be specified.
  • the values passed as 'show time' (interval for revealing notification), 'hang time' (time window remains awaiting user action), and 'hide time' (how quickly the window disappears) are assigned
  • if a valid file path was passed, the user's default notification sound is played
  • the work area of the screen (primary monitor) is determined. The work area is the screen real estate not occupied by a SHAppbar (like the taskbar and Office quick launch toolbars)
  • based on the placement parameter passed, SetWindowPos is called to move the notification form to the appropriate start location, the form is set to topmost, and - the key - is called with the No Activate flag.
  • a call to ShowWindow is made to reveal the form, again specifying ShowWindow's no-activate flag.
  • the timer's notify_mode is set to notify_mode_show (the other modes are notify_mode_wait and notify_mode_hide), the timer's 'show time' interval is set to that specified in the call, and the timer is enabled.

Up to this point a window of 0 height is on screen. Now a single timer takes over to perform the actual reveal/wait/hide:

  • when the notify_mode is notify_mode_show, each interval that fires the timers increases the height of the form until it reaches its desired height
  • when the form is set to the correct (end) height, the timer is stopped, the notify_mode is changed to notify_mode_wait, the timer's wait duration is assigned to the timer, and the timer is re-activated.
  • when the elapsed wait time elapses, the timer is again stopped, notify_mode is changed to notify_mode_hide, and the timer is re-enabled.
  • while in notify_mode_hide, each firing of the timer reduces the size of the notify form until the form is less than or equal to 0, at which time the timer is stopped and the notification is finished.

Because we want to perform an action should any part of the form be clicked, and because we want any mouse movement over the form or the label to cause the label to respond like a hyperlink, the MouseMove event of the label is routed to the MouseMove event of the form, which contains the code to highlight the label. SetCapture / ReleaseCapture ensures that when the cursor leaves the form, the hyperlink is restored to its normal non-hyperlink colour. The form's Form_MouseUp event (or Form_MouseDown if preferred) is used to detect clicks on the label or on the form itself. It would be in this event you would either execute the command desired, or set a return value the calling form could use to invoke a particular action. And regardless of the notify_mode, clicking the label or form instantly dismisses the dialog.

This demo shows several interesting techniques:

  • how to create a gradient form
  • how to draw an transparent bitmap on a form with transparency via the API
  • how to calculate usable screen area
  • how to display a VB form without activating it
  • how to capture mouse messages
  • how to use flags within a single timer to accomplish different things

VBnet thanks Pierre Alexis for providing the basis for this interesting code, and Pierre in turn extends his appreciation to François Picalausa and those in the newsgroups for helping him work through some of the issues in developing his original routine.

The layout form below shows the names of the respective controls you must add and their relative positions on the forms. First form is the calling form, while the second is the actual notify window.

This project is divided into three pages ... this page with the form design, the second with the calling form's code, and the third containing the actual notify window code.

Properties for the calling form can use the default names, including the default form name Form1. Into Image1 - a placeholder for the image in the notify window - load either the image provided below or another of your choosing. Note that the code for this demo sets the transparency colour to the red shown in the bitmap, so if another bitmap is used you will have to adjust the transparency colour appropriately. (You can use GetPixel to retrieve the precise colour of a point within the image if desired).

 

 

  

 

 

Properties for the notification form need some design-time adjusting. There is no titlebar, so set the Control Box to False, remove any Caption string, and set the form style to 3 - fixed dialog. ScaleMode can remain as the default Twips. Name the form frmNotify.

   


 Comments
Save the project and move on to Step 2: Building the Calling Form

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter