A tray icon
Some programs live in the notification area next to the clock: they hide their window, show an icon with a menu, and come back when the user double-clicks it. This lesson uses NotifyIcon, a context menu, Hide and Show, and a balloon message.
What you will learn#
NotifyIcon: the icon, the tip (Text) andVisible,- the icon (
SystemIcons), - a
ContextMenuStripwithOpenandQuit, Hide()andShow()for the window,- the balloon message (
ShowBalloonTip), - removing the icon when the program ends.
The tutorial Closing a window and the tutorial More than one window used Close, Hide and Show. A tray program hides its window instead of closing it.
The program#
# a tray icon - NotifyIcon: a program that lives in the notification area, a context menu, hiding the window, and a balloon message
load "dotnet.ring"
using {
"System"
"System.Drawing"
"System.Windows.Forms"
}
# controls and state that the event code uses are global variables
oForm = ""
oTray = ""
oMenu = ""
oStatus = ""
nShown = 0 # how many times the window was brought back from the tray
func main
oForm = uiForm("Tray demo",380,160)
oStatus = uiLabel(oForm,"The window is visible",20,20,330,24)
uiButton(oForm,"Hide to the tray",20,60,160,32,"hideToTray()")
# The tray icon needs an ICON (a small picture). SystemIcons has the standard ones, and an icon can also be made from a file.
oTray = new NotifyIcon()
oIcon = new SystemIcons().Application
oTray.Icon = oIcon
oTray.Text = "Tray demo (double-click to open)" # the tip when the mouse rests on the icon (63 characters at most)
oTray.Visible = True
# The icon has a context menu (right click): the same ContextMenuStrip that other controls have.
oMenu = new ContextMenuStrip()
oOpen = oMenu.Items.Add("Open")
oQuit = oMenu.Items.Add("Quit")
oOpen.setClickEvent("showFromTray()")
oQuit.setClickEvent("oForm.Close()")
oTray.ContextMenuStrip = oMenu
oTray.setDoubleClickEvent("showFromTray()")
# When the window closes the icon must be removed: it stays in the tray until the mouse moves over it if it is left there.
oForm.setFormClosedEvent("oTray.Visible = False oTray.Dispose()")
uiRun(oForm)
# hide the window: it is not minimized, it is gone from the screen and the taskbar; only the icon remains
func hideToTray
oForm.Hide()
if not uiSelfTest() # (a test does not show a message on the screen)
oTray.ShowBalloonTip([ :timeout = 2000, :tipTitle = "Tray demo", :tipText = "Still running in the tray", :tipIcon = "Info" ])
ok
func showFromTray
nShown++
oForm.Show(NULL)
oForm.Activate()
oStatus.Text = "Back from the tray " + nShown + " time(s)"How it works#
The icon#
A NotifyIcon needs an Icon: a small picture. new SystemIcons().Application is the standard application icon; a program with its own icon loads it from an .ico file. Text is the tip that appears when the mouse rests on the icon (at most 63 characters), and Visible = True puts the icon in the tray. An icon that is not visible is not shown at all.
The menu#
The right click shows the ContextMenuStrip of the icon. Items.Add("Open") adds an item and returns it, and setClickEvent gives it code. The double click on the icon has its own event, DoubleClick, and both call showFromTray. The menu item Quit closes the form, which ends the program.
Hide and show#
oForm.Hide() takes the window off the screen and off the taskbar, without closing it: the program goes on running, and the controls keep their values. Show(NULL) and Activate() bring it back in front. This is different from minimizing, where the window stays on the taskbar.
The balloon#
ShowBalloonTip shows a short message from the icon: a title, a text, an icon (Info, Warning, Error) and a time in milliseconds. Windows may show it as a notification and the user can switch these off, so a balloon is for information that is nice to have. The program does not show one in a test.
Remove the icon#
The tray keeps the icon of a program that ended without removing it, until the mouse moves over it. The FormClosed event sets Visible = False and calls Dispose() so the icon goes away with the program.
The test#
The test checks the icon, the tip and the menu, hides the window and checks that the window is hidden and the icon is not, then brings the window back through showFromTray and through the menu item (PerformClick). Nothing needs the real tray.
The checks of this program are in a separate file, selftest/ch10_tray.ring:
# the checks of ch10_tray.ring: run with ring ch10_tray.ring --selftest (the program loads this file and runs selfTest() once its window is shown)
func selfTest
dnAssert("the icon is visible",oTray.Visible,True)
dnAssert("the tip",oTray.Text,"Tray demo (double-click to open)")
dnAssert("two menu items",oMenu.Items.Count,2)
dnAssert("the first menu item",oMenu.Items.Item(0).Text,"Open")
# hide: the window is not visible, the icon still is
hideToTray()
dnAssert("the window is hidden",oForm.Visible,False)
dnAssert("the icon stays",oTray.Visible,True)
# the double click (or the menu item Open) brings it back
showFromTray()
dnAssert("the window is back",oForm.Visible,True)
dnAssert("the status",oStatus.Text,"Back from the tray 1 time(s)")
# the menu item runs the same function
oMenu.Items.Item(0).PerformClick()
dnAssert("the menu item Open",nShown,2)What you learned#
- a tray icon with a tip and a menu,
- hiding a window and bringing it back,
- the balloon message,
- removing the icon at the end.
Exercises#
- Hide the window instead of closing it when the user clicks the X (the event
FormClosingwithCancel; the tutorial Closing a window), and quit only from the menu. - Change the icon (draw one with
Graphicson a 16 by 16Bitmapand make anIconof it). - Add a menu item that shows the time in a balloon.
- Make the program start hidden in the tray.