如何使 Windows Forms .NET 应用程序显示为托盘图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/158895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:12:21  来源:igfitidea点击:

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

.netvb.netwinformssystem-traysystray

提问by steffenj

What needs to be done to have your .NET application show up in Window's system tray as icon?

需要做什么才能让您的 .NET 应用程序在 Window 的系统托盘中显示为图标?

And how do you handle mousebutton clicks on said icon?

你如何处理鼠标按钮点击所述图标?

回答by tom.dietrich

First, add a NotifyIconcontrol to the Form. Then wire up the Notify Icon to do what you want.

首先,向 Form添加一个NotifyIcon控件。然后连接通知图标以执行您想要的操作。

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)

回答by Carl

You can add the NotifyIcon component from the toolbox onto your main form.

您可以将工具箱中的 NotifyIcon 组件添加到主窗体中。

This has events such as MouseDoubleClick that you can use to handle various events.

它具有诸如 MouseDoubleClick 之类的事件,您可以使用它们来处理各种事件。

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.

编辑:如果您希望它在系统托盘中正确显示,您必须确保将 Icon 属性设置为有效的 .ico 文件。

回答by GEOCHET

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

关于在此处使用 NotifyIcon 类的不错的小教程:http: //www.developer.com/net/csharp/article.php/3336751

回答by Vivek

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

NotifyIcon组件添加到您的表单中。并使用它的事件来处理鼠标点击。

回答by Vivek

This shows and handles all the mouse click combinations for NotifyIcon

这显示并处理 NotifyIcon 的所有鼠标点击组合

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

更多信息:https: //archive.codeplex.com/?p=notifyicon

回答by VoteCoffee

To extend Tom's answer, I like to only make the icon visible if the application is minimized.
To do this, set Visible = Falsefor NotifyIconand use the below code.

为了扩展汤姆的回答,我喜欢只在应用程序最小化时才使图标可见。
要做到这一点,设置Visible = FalseNotifyIcon的,并使用下面的代码。

I also have code below to hide the icon during close the prevent the annoying ghosttray 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

VB.NET:如何为托盘图标制作右键菜单

Per the article (with mods for context):

根据文章(带有上下文的 mods):

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").
  • 在属性中将 FormBorderStyle 设置为 None。
  • 将 ShowInTaskbar 设置为 False(因为当我们右键单击托盘图标时,我们不希望任务栏中出现图标!)。
  • 将起始位置设置为手动。
  • 将 TopMost 设置为 True。
  • 将 ContextMenuStrip 添加到您的新表单,并根据需要为其命名。
  • 向 ContextMenuStrip 添加项目(对于本示例,只需添加一项名为“退出”的项目)。

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 (TrayIconMenuFormis the name of my Form for providing the context menu):

然后我将通知图标鼠标事件更改为此(TrayIconMenuForm是我提供上下文菜单的表单的名称):

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