[ACCEPTED]-How to make a Windows Forms .NET application display as tray icon?-systray
First, add a NotifyIcon control to the Form. Then 5 wire up the Notify Icon to do what you want.
If 4 you want it to hide to tray on minimize, try 3 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 2 in order to notify a user - that is done 1 as such:
Me.NotifyIcon1.ShowBalloonTip(3000, "This is a notification title!!", "This is notification text.", ToolTipIcon.Info)
You can add the NotifyIcon component from 6 the toolbox onto your main form.
This has 5 events such as MouseDoubleClick that you 4 can use to handle various events.
Edit: You 3 have to make sure that you set the Icon 2 property to a valid .ico file if you want 1 it to show up properly in the systray.
To extend Tom's answer, I like to only make the icon 12 visible if the application is minimized.
To 11 do this, set
Visible = False for NotifyIcon and use the below code.
I 10 also have code below to hide the icon during 9 close the prevent the annoying ghost tray icons 8 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 7 you want to add a right click menu:
VB.NET: How to Make a Right Click Menu for a Tray Icon
Per the 6 article (with mods for context):
Setting 5 up the Form for hosting the tray icon context 4 menu
- In the Properties set FormBorderStyle to None.
- Set ShowInTaskbar as False (because we don't want an icon appearing in taskbar when we right-click the tray icon!).
- Set StartPosition to Manual.
- Set TopMost to True.
- Add a ContextMenuStrip to your new Form, and name it whatever you want.
- Add items to the ContextMenuStrip (for this example just add one item called "Exit").
The Form code behind will look like 3 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 2 event to this (
TrayIconMenuForm is the name of my Form for 1 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
More Related questions