![](/img/trans.png)
[英]How can I make a .NET Windows Forms application that only runs in the System Tray?
[英]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的所有鼠標單擊組合
為了擴展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
如果要添加右鍵菜單:
根據文章(使用上下文的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.