20

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

以及如何处理鼠标按钮单击所述图标?

4

3 回答 3

22

首先,向窗体添加一个NotifyIcon控件。然后连接通知图标来做你想做的事。

如果你想让它在最小化时隐藏到托盘,试试这个。

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

我偶尔会使用气球文本来通知用户——这样做是这样的:

 Me.NotifyIcon1.ShowBalloonTip(3000, "This is a notification title!!", "This is notification text.", ToolTipIcon.Info)
于 2008-10-01T18:08:12.163 回答
6

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

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

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

于 2008-10-01T18:02:53.423 回答
1

为了扩展汤姆的回答,我希望仅在应用程序最小化时才使图标可见。
为此,请设置Visible = FalseNotifyIcon使用以下代码。

我还有下面的代码可以在关闭期间隐藏图标,以防止在应用程序关闭后仍然存在的烦人的幽灵托盘图标。

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

如果要添加右键菜单:

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

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

设置用于托管托盘图标上下文菜单的表单

  • 在属性中将 FormBorderStyle 设置为无。
  • 将 ShowInTaskbar 设置为 False(因为当我们右键单击托盘图标时,我们不希望任务栏中出现图标!)。
  • 将开始位置设置为手动。
  • 将 TopMost 设置为 True。
  • 将 ContextMenuStrip 添加到您的新表单中,然后将其命名为您想要的任何名称。
  • 将项目添加到 ContextMenuStrip(对于此示例,只需添加一个名为“Exit”的项目)。

后面的表单代码将如下所示:

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

然后我将 notifyicon 鼠标事件更改为此(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
于 2018-04-02T19:12:53.297 回答