要让您的.NET应用程序在Windows系统托盘中显示为图标,需要做些什么?
你如何处理所述图标上的鼠标按钮?
答案 0 :(得分:20)
首先,向表单添加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)
答案 1 :(得分:6)
您可以将工具箱中的NotifyIcon组件添加到主表单中。
这可以使用MouseDoubleClick等事件来处理各种事件。
编辑:如果您希望它在系统托盘中正确显示,您必须确保将Icon属性设置为有效的.ico文件。
答案 2 :(得分:2)
在这里使用NotifyIcon类的很好的小教程:http://www.developer.com/net/csharp/article.php/3336751
答案 3 :(得分:0)
将NotifyIcon组件添加到表单中。并使用它的事件来处理鼠标点击。
答案 4 :(得分:0)
显示并处理NotifyIcon的所有鼠标点击组合
答案 5 :(得分:0)
要扩展Tom's answer,我希望只在应用程序最小化时才显示图标
为此,请为NotifyIcon设置Visible = False
并使用以下代码。
我也有以下代码在关闭期间隐藏图标,以防止在应用程序关闭后仍然存在恼人的 ghost 托盘图标。
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: How to Make a Right Click Menu for a Tray Icon
根据文章(上下文的mod):
设置用于托管托盘图标上下文菜单的表单
背后的表单代码如下所示:
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