Posted by: roup21 | October 31, 2008

Sys try

The world is not short of VB SysTray applications. Many use subclassing controls, which are complicated, prone to crashing and unnecessary. One of the tray form’s standard events can be re- directed to receive mouse click events over the systray. The event usually used is MouseMove. I wanted an application which encapsulated all of the functions usually required for systray applications into a single form, which can easily be included in projects. The main features required are:

* Events when the tray icon is clicked.
* A popup menu.
* The ability easily to set the tray icon and tooltip.
* The ability to intercept form resize and queryunload events.

The application works as follows:

The main application form is loaded; it loads an instance of ‘Frmsystray’. Frmsystray is initially- and remains- invisible. Frmsystray has a public form variable (FSYS) which is set to reference the main application form. This means that Frmsystray can manipulate the main form, e.g. making it visible, maximized, minimized etc. in response to mouse events.

Frmsystray also exposes:

(1) A tooltip (string) which sets the tray tooltip

(2) A ‘TrayIcon’ property. This sets the Frmsystray form icon, which in turn sets the tray icon. This may be set to a picturebox, Image, form or string.
If picturebox or image, the tray icon is set to the respective picture. If a form, the tray icon is set to the form’s icon. If a string, if “Default” (case insensitive), the FrmSysTray form icon is set alternately to the “flash1″ and “flash2″ pictures on FrmSysTray. Which it is set to is changed when a timer on FrmSysTray fires (default interval 1000 ms). This is the default behaviour. If the string is not “Default”, then it is assumed to be a picture or icon filename and it is loaded.

(3) An Interval integer. This sets the timer interval. Setting to zero disabled the icon flashing.

(4) A ‘Click’ event. This is generated whenever the tray icon is clicked, and passes a string describing what happened. By default when the right button is clicked a menu pops up which can resize the main form. When the left button is double clicked the main form is shown.

(5) A ‘TIcon’ event. This is fired whenever the tray icon is updated. It passes the FrmSysTray form, whose icon is the same as the tray icon.

(6) A MeResize procedure. This can optionally be called from the main form’s resize event. It causes the main form to be hidden when minimized, a common feature of systray applications.

(6) A MeQueryUnload procedure. This can similarly be optionally called from the main form QueryUnload event. It causes the main form’s unloading to be cancelled if unloadmode is vbFormControlMenu, i.e. if the user chose the Close command from the Control menu on the form. In that case the form is minimized and hidden. Again, this is a common feature of SysTray applications. To end the program therefore, another form unload method must be invoked, e.g. use of the Unload statement.

The example program has the FrmSysTray form, and three different example startup forms. frmMainSimple has just about the minimum code required to set a flashing icon in the tray, and does nothing else. FrmMainIntermediate has a little more functionality, uses the MeResize and MeQueryUnload procedures, and the TIcon event. frmMainComplicated exposes all of the features of the program; it allows the tray icon to be set from a form, picture or image, and to the default. The update interval can be changed, as can the tooltip. The default icons can be directly changed also. The MeResize and MeQueryUnload procedures can optionally be called.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.