繁体   English   中英

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

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

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

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

首先,将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)

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

这包含可用于处理各种事件的MouseDoubleClick等事件。

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

关于在这里使用NotifyIcon类的很好的小教程: http//www.developer.com/net/csharp/article.php/3336751

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

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

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

为了扩展Tom的答案 ,我喜欢只在应用程序最小化时才显示图标。
为此,请为NotifyIcon设置Visible = False并使用以下代码。

我也有下面的代码隐藏图标在关闭期间防止应用程序关闭后持续存在的恼人的托盘图标。

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:如何为托盘图标制作右键菜单

根据文章(使用上下文的mod):

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

  • 在Properties中将FormBorderStyle设置为None。
  • 将ShowInTaskbar设置为False(因为当我们右键单击托盘图标时,我们不希望任务栏中出现图标!)。
  • 将StartPosition设置为手动。
  • 将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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM