Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Building The Control

The source code for the System Tray Icon Control is located on the Visual Basic 5 CD in the \tools\unsupprt\systray directory. These files have their "read-only" property set to True, so we’ll need to relocate them to the hard drive and change their properties.

Follow these steps to build the control:

 

1 Copy the files in the \tools\unsupprt\systray directory from the CD to a directory on the local hard drive. I used e:\vb\Projects\Systray for this particular project.

2 Highlight all of the files in the new directory. Right-click on the list and select "Properties" from the resulting pop-up menu.

3 Clear the "Read-only" check box on the General tab and click "OK".

4 From the Visual Basic 5 IDE, open the project file Systray.vbp. You’ll notice that there are two files within the project, the module mSystray and the UserControl cSystray.

5 From the File menu, select "Make SysTray.ocx…". In the resulting "Make Project" dialog box, the selected directory will be the project directory. Change the directory in order to make the file as c:\windows\system\SysTray.ocx and click "OK".

The control will then be listed as in the Components dialog as "System Tray Icon Control". Select this item in the Chapter6 project so we can use it.

Properties

As I said, the System Tray Icon Control has very few properties (and no functions):

Property

Description

Name

Name given to the control

Index

Returns/sets the number identifying the control in a control array

Left

Returns/sets the distance between the left edge of the control and its container

Tag

Stores any extra data

Top

Returns/sets the distance between the top edge of the control and its container

InTray

Whether or not the icon is currently visible in the tray

TrayIcon

Returns/sets the icon used with the control

TrayTip

Returns/sets the control’s tooltip

 

Events

There are only four events for the control itself, all related to the mouse:

Event

Description

MouseDblClick

Occurs when a mouse button is double-clicked

MouseDown

Occurs at the end of the down-stroke of a mouse button

MouseMove

Occurs when the mouse is moved over the icon in the system tray

MouseUp

Occurs at the end of the up-stroke of a mouse button

However, since the three events other than MouseMove also identify the mouse button that was clicked, that makes for seven individual events … all from single system tray icon!

Try It Out - The System Tray Icon Control

Setting-Up The Form

Place the System Tray Icon Control onto our form in the Chapter6 project:

The first thing we need to do is set the form’s visible property to False, so the icon shows up in the system tray, ready to make the form appear. Place a menu named Calendar, with the handle of mnuCalendar, onto the form. Give this menu three items: Show Calendar (mnuShowCalendar), mnuBar1, and Exit (mnuExit):

Now, go back into the menu editor and set the Visible property for mnuCalendar to False. Doing so hides this menu, making it a candidate for a popup menu.

The Show Calendar menu item will be used for causing the calendar form itself to be displayed. Place the following code under the mnuShowCalendar_Click event:

Private Sub mnuShowCalendar_Click()

  ' Show the Calendar form
  Form1.Visible = True

End Sub

This next piece of code goes under the mnuExit_Click event, to do exactly what it says:

Private Sub mnuExit_Click()

  ‘ Exit the program
  End

End Sub

Back on the File menu, this is the code for the mnuFileClose_Click event. This causes the form to disappear, making it available for later. Notice this doesn’t cause the program to end, so the icon will still be visible in the system tray:

Private Sub mnuFileClose_Click()

  ' Make the form invisible
  Form1.Visible = False

End Sub

And lastly, this code goes under the control’s MouseDown event. It causes the Calendar menu to pop up over the icon on the left-click event:

Private Sub cSysTray1_MouseDown(Button As Integer, Id As Long)

  Select Case Button
    Case 1  ' Left button
      PopupMenu mnuCalendar
    Case 2  'Right button
      ' Do nothing
  End Select

End Sub

Setting the Control’s Properties

The control’s properties are set as shown:

The icon is in the tray by default when the setting for the InTray property is True. The icon itself is set as needed (I used trffc10a.ico in the VB\Graphics\Icons\Traffic directory to get the traffic light icon), and the TrayTip property is loaded with the string we want to be displayed when the mouse hovers over the icon, "Chapter 6 Sample".

Running the Program

Start the program from the Run menu in the VB IDE and the icon will appear in the system tray. Notice that the form does not appear. Hover the mouse over the icon and the tray tip will appear:

Clicking the left mouse button on the icon will cause the popup menu to appear:

Clicking on Show Calendar will show the form as we’ve been using it all along. Use the File | Close menu item to close the form, leaving the icon active in the system tray. Clicking on the Exit menu item on the popup menu will carry out the End command and close our program.

 

System Tray Icon Control GUI Notes

The System Tray Icon is a great addition to the GUI environment. It’s small, out of the way, doesn’t do anything until you need it to, and it’s always there. But be careful. Too much of a good thing will always ruin it. On a monitor set for 640x480 pixels, I’ve found confusion after about 4 icons in the system tray. Larger resolutions (600 x 800, 1024x768, or even higher) can support more without much more confusion, but don’t overdo it.

Summary

At this point you should be ready to tackle more of the unsupported controls and utilities provided on the Visual Basic CD. In this paper we’ve covered:

  • A calendar control with extensive capabilities that we can change for our own custom uses

  • A better set of methods for implementing most of the common dialogs

  • A control specifically used for implementing icons in the system tray



  • [Home Page] [Tutorials] [Previous Lesson] [Dave Liske's Site] [Wrox Web-Developer]





    Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

    Quick searches: Site Search | Advanced Site Search 

    Copyright 2002 by Exhedra Solutions, Inc.
    By using this site you agree to its terms and conditions
    VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.