需要做让你的.NET应用程序显示在窗口的系统托盘为图标怎么办?
你怎么在上述图标处理mousebutton点击?
需要做让你的.NET应用程序显示在窗口的系统托盘为图标怎么办?
你怎么在上述图标处理mousebutton点击?
首先,添加一个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,你可以用它来处理各种事件。
编辑:你必须确保你,如果你希望它在系统托盘正确显示设置图标属性,以一个有效的.ico文件。
在这里使用的NotifyIcon类可爱的小教程: http://www.developer.com/net/csharp/article.php/3336751
添加NotifyIcon的组件添加到您的形式。 并用它的事件来处理鼠标点击。
这说明,并处理所有的NotifyIcon的鼠标点击组合
这里更多: https://archive.codeplex.com/?p=notifyicon
为了延长汤姆的回答 ,我想只会使图标可见,如果应用程序最小化。
要做到这一点,设置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:如何使一个右键菜单的托盘图标
每文章(MODS与上下文):
设置表单主办托盘图标右键菜单
表单代码隐藏看起来就像这样:
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