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

发布于 2024-07-06 16:12:05 字数 77 浏览 8 评论 0原文

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

您如何处理该图标上的鼠标按钮点击?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咿呀咿呀哟 2024-07-13 16:12:05

首先,向形式。 然后连接通知图标以执行您想要的操作。

如果您希望它隐藏在最小化托盘上,请尝试此操作。

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)

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)
空城旧梦 2024-07-13 16:12:05

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

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

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

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.

子栖 2024-07-13 16:12:05

为了扩展Tom的回答,我喜欢仅在应用程序最小化时使图标可见。
为此,请为 NotifyIcon 并使用以下代码。

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

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 设置为 None。
  • 将 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

然后,我将 notificationicon 鼠标事件更改为此(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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文