|In this article we will learn how to implement Balloon ToolTip with Sound notification and System Tray with Context menu in VB.net|
Since there is no inbuilt classes in .net to implement Balloon ToolTip so we have to do this using API.
This code contains 2 important classes
CToolTipNotify - To create System Tray icon and manage events system tray events.
CSound - To play system sound, wave file stored in resource and any user defined wave file.
CToolTipNotify class member information
Methods :Events :
- Click - Event is fired when user click on the System Tray Icon
- DoubleClick - Event is fired when user double click on the System Tray Icon
- MouseDown - Event is fired when user click on the System Tray Icon (This event gives you Button Parameter)
- MouseMove - Event is fired when user moves mouse on System Tray Icon
- MouseUp - Event is fired when user release mouse button from System Tray Icon
- Reload - Event is fired when taskbar is created
- BalloonShow - Event is fired when System Tray Balloon ToolTip appears
- BalloonHide - Event is fired when System Tray Balloon ToolTip disappears
- BalloonTimeout - Event is fired when timeout occurs for System Tray Balloon ToolTip
- BalloonClick - Event is fired when user click on System Tray Balloon ToolTip
- Icon - Icon of System Tray
- Text - Message text for Balloon ToolTip
- ContextMenu - Assign Context menu object for System Tray Icon
- Visible - Show/Hide System Tray Icon
CSound class member information
- PlayWavFile - This method plays wave file from a specified path
- PlaySystemSound - This method plays system standard event sounds which are found in WIN.INI
- PlayWavResource - This method plays wave file stored as a resource in exe or dll
Shell_NotifyIcon API is used to create System tray icon. Here is the VB.net declaration for Shell_NotifyIcon
[in] Variable of type DWORD that specifies the action to be taken. It can have one of the following values.
- NIM_ADD : Adds an icon to the status area. The hWnd and uID members of the NOTIFYICONDATA structure pointed to by lpdata will be used to identify the icon in later calls to Shell_NotifyIcon.
- NIM_DELETE : Deletes an icon from the status area. Use the hWnd and uID members of the NOTIFYICONDATA structure pointed to by lpdata to identify the icon to be deleted.
- NIM_MODIFY : Modifies an icon in the status area. Use the hWnd and uID members of the NOTIFYICONDATA structure pointed to by lpdata to identify the icon to be modified.
- NIM_SETFOCUS : Version 5.0. Returns focus to the taskbar notification area. Taskbar icons should use this message when they have completed their user interface operation. For example, if the taskbar icon displays a shortcut menu, but the user presses ESC to cancel it, use NIM_SETFOCUS to return focus to the taskbar notification area.
- NIM_SETVERSION : Version 5.0. Instructs the taskbar to behave according to the version number specified in the uVersion member of the structure pointed to by lpdata. This message allows you to specify whether you want the version 5.0 behavior found on Microsoft Windows 2000 systems, or that found with earlier Shell versions. The default value for uVersion is zero, indicating that the original Windows 95 notify icon behavior should be used. For details, see the Remarks section.
[in] Pointer to a NOTIFYICONDATA structure. The content of the structure depends on the value of dwMessage.
Returns TRUE if successful, or FALSE otherwise. If dwMessage is set to NIM_SETVERSION, the function returns TRUE if the version was successfully changed, or FALSE if the requested version is not supported.
Second parameter is lpdata which is pointer to NOTIFYICONDATA structure which holds all information about System Tray Icon. Here is the VB.net declaration of NOTIFYICONDATA
| Click here to copy the following block|
|<StructLayout(LayoutKind.Sequential)> Private Structure NOTIFYICONDATA|
Dim cbSize As Int32
Dim hwnd As IntPtr
Dim uID As Int32
Dim uFlags As Int32
Dim uCallbackMessage As Int32
Dim hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Dim szTip As String
Dim dwState As Int32
Dim dwStateMask As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Dim szInfo As String
Dim uVersion As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> Dim szInfoTitle As String
Dim dwInfoFlags As Int32
Size of this structure, in bytes.
Handle to the window that receives notification messages associated with an icon in the taskbar status area. The Shell uses hWnd and uID to identify which icon to operate on when Shell_NotifyIcon is invoked.
Application-defined identifier of the taskbar icon. The Shell uses hWnd and uID to identify which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons associated with a single hWnd by assigning each a different uID.
Flags that indicate which of the other members contain valid data. This member can be a combination of the following.
- NIF_ICON : The hIcon member is valid.
- NIF_MESSAGE : The uCallbackMessage member is valid.
- NIF_TIP : The szTip member is valid.
- NIF_STATE : The dwState and dwStateMask members are valid.
- NIF_INFO : Use a balloon ToolTip instead of a standard ToolTip. The szInfo, uTimeout, szInfoTitle, and dwInfoFlags members are valid.
- NIF_GUID : Reserved.
Application-defined message identifier. The system uses this identifier to send notifications to the window identified in hWnd. These notifications are sent when a mouse event occurs in the bounding rectangle of the icon, or when the icon is selected or activated with the keyboard. The wParam parameter of the message contains the identifier of the taskbar icon in which the event occurred. The lParam parameter holds the mouse or keyboard message associated with the event. For example, when the pointer moves over a taskbar icon, lParam is set to WM_MOUSEMOVE. See the Taskbar guide chapter for further discussion.
Handle to the icon to be added, modified, or deleted. To avoid icon distortion, be aware that notification area icons have different levels of support under different versions of Microsoft Windows. Windows 95, Windows 98, and Microsoft Windows NT 4.0 support icons of up to 4 bits per pixel (BPP). Windows Millennium Edition (Windows Me) and Windows 2000 support icons of a color depth up to the current display mode. Windows XP supports icons of up to 32 BPP.
Pointer to a null-terminated string with the text for a standard ToolTip. It can have a maximum of 64 characters including the terminating NULL.
For Version 5.0 and later, szTip can have a maximum of 128 characters, including the terminating NULL.
Version 5.0. State of the icon. There are two flags that can be set independently.dwStateMask
Version 5.0. A value that specifies which bits of the state member are retrieved or modified. For example, setting this member to NIS_HIDDEN causes only the item's hidden state to be retrieved.
Version 5.0. Pointer to a null-terminated string with the text for a balloon ToolTip. It can have a maximum of 255 characters. To remove the ToolTip, set the NIF_INFO flag in uFlags and set szInfo to an empty string.
Union with uVersion. The timeout value, in milliseconds, for a balloon ToolTip. The system enforces minimum and maximum timeout values. uTimeout values that are too large are set to the maximum value and values that are too small default to the minimum value. The system minimum and maximum timeout values are currently set at 10 seconds and 30 seconds, respectively. See the remarks for further discussion of uTimeout.
Version 5.0. Union with uTimeout. Specifies whether the Shell notify icon interface should use Windows 95 or Windows 2000 behavior. For more information on the differences in these two behaviors, see Shell_NotifyIcon. This member is only employed when using Shell_NotifyIcon to send a NIM_SETVERSION message.
0 Use the Windows 95 behavior. Use this value for applications designed for Windows versions prior to Windows 2000.
NOTIFYICON_VERSION : Use the Windows 2000 behavior. Use this value for applications designed for Windows 2000 and later.
Version 5.0. Pointer to a null-terminated string containing a title for a balloon ToolTip. This title appears in boldface above the text. It can have a maximum of 63 characters.
Version 5.0. Flags that can be set to add an icon to a balloon ToolTip. It is placed to the left of the title. If the szInfoTitle member is zero-length, the icon is not shown.
- NIIF_ERROR : An error icon.
- NIIF_INFO : An information icon.
- NIIF_NONE : No icon.
- NIIF_USER : Windows XP Service Pack 2 (SP2) and later. Use the icon identified in hIcon as the notification balloon's title icon.
- NIIF_WARNING : A warning icon.
- NIIF_ICON_MASK : Version 6.0. Reserved.
- NIIF_NOSOUND : Version 6.0. Do not play the associated sound. Applies only to balloon ToolTips.
Submitted By :
(Member Since : 5/26/2004 12:23:06 PM)
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author
(Birth Date : 7/14/1981 )