How to make a Windows Forms .NET application display as tray icon?

前端 未结 6 1992
孤城傲影
孤城傲影 2020-12-03 07:44

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?

相关标签:
6条回答
  • 2020-12-03 07:53

    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.

    0 讨论(0)
  • 2020-12-03 07:53

    Nice little tutorial on using the NotifyIcon class here: http://www.developer.com/net/csharp/article.php/3336751

    0 讨论(0)
  • 2020-12-03 07:54

    This shows and handles all the mouse click combinations for NotifyIcon

    More here: https://archive.codeplex.com/?p=notifyicon

    0 讨论(0)
  • 2020-12-03 07:56

    Add NotifyIcon component to your form. And use it's events to handle mouse clicks.

    0 讨论(0)
  • 2020-12-03 08:09

    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)
    
    0 讨论(0)
  • 2020-12-03 08:09

    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

    • 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 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
    
    0 讨论(0)
提交回复
热议问题