How To Use the System Tray Directly from Visual Basic
This article was previously published under Q176085
On This Page
SUMMARY | |||||
MORE INFORMATION | |||||
Step-by-Step Example | |||||
Taskbar Notification Area Flexibility | |||||
REFERENCES | |||||
APPLIES TO |
SUMMARY
This article demonstrates how to take full advantage of the Windows System Tray, or Taskbar Notification Area, using Visual Basic. It places an icon of your choice into the Taskbar Notification Area that will display a ToolTip of your choice when the mouse is rested over it, will restore the application when clicked, and will display a popup menu when right-clicked. This is all possible because of Visual Basic's ability to directly handle callbacks, therefore taking full advantage of the Shell_NotifyIcon function that is exported by Shell32.dll.
MORE INFORMATION
The following example can be added to any Visual Basic Project that has at least one form and a standard module.
You can add additional menu items as needed.
You can modify the Icon that appears in the Taskbar Notification Area by changing the following line in the Form_Load procedure:
You can change any of the Taskbar Notification Area settings at any time after the use of the NIM_ADD constant by reassigning the values in the nid variable and then using the following variation of the Shell_NotifyIcon API call:
Step-by-Step Example
1. | Add the following code to the declarations section of a standard module in your project:
|
2. | Add the following code to any form in your project that you want to respond to the System Tray Icon, or Notification Icon, for your application:
|
3. | Make the following Property Settings on the same form to which you added the above code:Property Required Setting for Taskbar Notification Area example ----------------------------------------------------------------------- Icon = The icon you want to appear in the system tray. Minbutton = True ShownInTaskbar = False |
4. | Add the following Menu items to the same form using the Menu Editor:Caption Name Enabled Visible Position --------------------------------------------------------- &SysTray mPopupSys True False Main Level &Restore mPopRestore True True Inset one &Exit mPopExit True True Inset one |
Taskbar Notification Area Flexibility
You can modify the ToolTip that appears over the Notification icon by changing the following line in the Form_Load procedure: .szTip = "Your ToolTip" & vbNullChar
Replace "Your ToolTip" with the text that you want to appear.You can modify the Icon that appears in the Taskbar Notification Area by changing the following line in the Form_Load procedure:
.hIcon = Me.Icon
Replace Me.Icon with any Icon in your project.You can change any of the Taskbar Notification Area settings at any time after the use of the NIM_ADD constant by reassigning the values in the nid variable and then using the following variation of the Shell_NotifyIcon API call:
Shell_NotifyIcon NIM_MODIFY, nid.
However, if you want a different form to receive the callback, then you will need to delete the current icon first using "Shell_NotifyIcon NIM_Delete, nid" as the NIM_Modify function will not accept a new Hwnd, or you will need to add another Icon to the systray for the new form using "Shell_NotifyIcon NIM_ADD, nid" after refilling the nid type with the new forms Hwnd. You can also declare separate copies of the nid type for each form that you want to display an icon for in the Windows System Tray and change them in each form's activate event using the NIM_DELETE and NIM_ADD sequence.
REFERENCES
For information regarding using the System Tray, or Taskbar Notification Area, from Visual Basic 4.0 or earlier, please see the following article in the Microsoft Knowledge Base:
149276 (http://support.microsoft.com/kb/149276/EN-US/) How To Use Icons with the Windows 95 System Tray
posted on 2005-07-24 16:27 LabVIEW开发者 阅读(543) 评论(0) 编辑 收藏 举报