What needs to be done to have your .NET application show up in Window's system tray as icon?
And how do you handle mousebutton clicks on said icon?
What needs to be done to have your .NET application show up in Window's system tray as icon?
And how do you handle mousebutton clicks on said icon?
First, add a NotifyIcon control to the Form. Then wire up the Notify Icon to do what you want.
If you want it to hide to tray on minimize, try this.
Private Sub frmMain_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.ShowInTaskbar = False
Else
Me.ShowInTaskbar = True
End If
End Sub
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
Me.WindowState = FormWindowState.Normal
End Sub
I'll occasionally use the Balloon Text in order to notify a user - that is done as such:
Me.NotifyIcon1.ShowBalloonTip(3000, "This is a notification title!!", "This is notification text.", ToolTipIcon.Info)
You can add the NotifyIcon component from the toolbox onto your main form.
This has events such as MouseDoubleClick that you can use to handle various events.
Edit: You have to make sure that you set the Icon property to a valid .ico file if you want it to show up properly in the systray.
Nice little tutorial on using the NotifyIcon class here: http://www.developer.com/net/csharp/article.php/3336751
Add NotifyIcon component to your form. And use it's events to handle mouse clicks.
This shows and handles all the mouse click combinations for NotifyIcon
More here: https://archive.codeplex.com/?p=notifyicon
To extend Tom's answer, I like to only make the icon visible if the application is minimized.
To do this, set Visible = False
for NotifyIcon and use the below code.
I also have code below to hide the icon during close the prevent the annoying ghost tray icons that persist after application close.
Private Sub Form_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Hide()
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(3000, NotifyIcon1.Text, "Minimized to tray", ToolTipIcon.Info)
End If
End Sub
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
Show()
Me.WindowState = FormWindowState.Normal
Me.Activate()
NotifyIcon1.Visible = False
End Sub
Private Sub Form_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
NotifyIcon1.Visible = False
Dim index As Integer
While index < My.Application.OpenForms.Count
If My.Application.OpenForms(index) IsNot Me Then
My.Application.OpenForms(index).Close()
End If
index += 1
End While
End Sub
If you want to add a right click menu:
VB.NET: How to Make a Right Click Menu for a Tray Icon
Per the article (with mods for context):
Setting up the Form for hosting the tray icon context menu
The Form code behind will look like this:
Private Sub Form_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
Me.Close()
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ContextMenuStrip1.Show(Cursor.Position)
Me.Left = ContextMenuStrip1.Left + 1
Me.Top = ContextMenuStrip1.Top + 1
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
MainForm.NotifyIcon1.Visible = False
End
End Sub
I then change the notifyicon mouse event to this (TrayIconMenuForm
is the name of my Form for providing the context menu):
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
Select Case e.Button
Case Windows.Forms.MouseButtons.Left
Show()
Me.WindowState = FormWindowState.Normal
Me.Activate()
NotifyIcon1.Visible = False
Case Windows.Forms.MouseButtons.Right
TrayIconMenuForm.Show() 'Shows the Form that is the parent of "traymenu"
TrayIconMenuForm.Activate() 'Set the Form to "Active", that means that that will be the "selected" window
TrayIconMenuForm.Width = 1 'Set the Form width to 1 pixel, that is needed because later we will set it behind the "traymenu"
TrayIconMenuForm.Height = 1 'Set the Form Height to 1 pixel, for the same reason as above
Case Else
'Do nothing
End Select
End Sub