Tom's answer
,我只想在最小化应用程序时使图标可见。
要执行此操作,请设置
Visible = False
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: How to Make a Right Click Menu for a Tray Icon
设置用于承载托盘图标上下文菜单的表单
-
在属性中,将FormBorderStyle设置为“无”。
-
将ShowInTaskbar设置为False(因为我们不希望在右键单击任务栏图标时任务栏中出现图标!)。
-
将StartPosition设置为手动。
-
将TopMost设置为True。
-
将ContextMenuStrip添加到新表单中,并根据需要命名它。
-
后面的表单代码如下所示:
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