これ、自分のデスクトップではよく見かけますね。ウィルスバスターとか。
一般的な使い方は 画面が閉じたときとか最小化した時にタスクトレイにしまっておいて、
ContextMenu やら ContextMenuStrip で何かしたり、そいつをクリックしたりすると びこっ と出てくる、って使い方が普通なんでしょうか。
タスクトレイにごちゃごちゃ何かあるのを嫌う人もいますしね。
使いどころは微妙...?なのかしら?
■参考文献
NotifyIcon クラス
NotifyIcon コンポーネント (Windows フォーム)
CloseReason 列挙体
■実行画像
ポコッと BaloonTip が出たところ
Public Class NotifyIconTest
' ContextMenuStrip の Close が押下されたかどうかのフラグ
Private m_closedMenuClicked As Boolean = False
Private Sub NotifyIconTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' ContextMenuStrip の設定
Dim context As ContextMenuStrip = New ContextMenuStrip()
Dim beepMenu As ToolStripMenuItem = New ToolStripMenuItem()
context.Items.Add(beepMenu)
beepMenu.Text = "Beep"
AddHandler beepMenu.Click, AddressOf OnBeepMenuClick
Dim showMenu As ToolStripMenuItem = New ToolStripMenuItem()
context.Items.Add(showMenu)
showMenu.Text = "Show"
AddHandler showMenu.Click, AddressOf OnShowMenuClick
Dim closeMenu As ToolStripMenuItem = New ToolStripMenuItem()
context.Items.Add(closeMenu)
closeMenu.Text = "Close"
AddHandler closeMenu.Click, AddressOf OnCloseMenuClick
' NotifyIcon の設定
With Me.NotifyIcon1
.BalloonTipIcon = ToolTipIcon.Info
.BalloonTipText = "ふじことえでん"
.BalloonTipTitle = "Fujiko Eden"
.ContextMenuStrip = context
.Icon = My.Resources.wankumaIcon
.Text = "ツールヒントテキスト"
.Visible = False ' 最初は非表示にしておく
End With
End Sub
' 画面を閉じたときに NotifyIcon を表示する
Private Sub NotifyIconTest_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason <> CloseReason.UserClosing Then Return
' ただし、Close を押下して閉じようとする場合は閉じる
If Me.m_closedMenuClicked Then Return
' それ以外は NotifyIcon を表示する
e.Cancel = True
Me.Visible = False
Me.NotifyIcon1.Visible = True
Me.NotifyIcon1.ShowBalloonTip(10000) ' 10 秒
End Sub
' 画面のサイズ変更時(最小化の時だけ)に NotifyIcon を表示する
Private Sub NotifyIconTest_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = False
Me.NotifyIcon1.Visible = True
Me.NotifyIcon1.ShowBalloonTip(10000, "Resize Title", "Resize", ToolTipIcon.Warning) ' 10 秒
End If
End Sub
'' ContextMenuStrip のイベントハンドラ
Private Sub OnBeepMenuClick(ByVal sender As Object, ByVal e As System.EventArgs)
Microsoft.VisualBasic.Interaction.Beep()
End Sub
Private Sub OnShowMenuClick(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Visible = True
Me.WindowState = FormWindowState.Normal
Me.NotifyIcon1.Visible = False
End Sub
Private Sub OnCloseMenuClick(ByVal sender As Object, ByVal e As System.EventArgs)
Me.NotifyIcon1.Visible = False
Me.m_closedMenuClicked = True
Me.Close()
End Sub
'' NotifyIcon のイベントハンドラ
Private Sub NotifyIcon1_BalloonTipClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.BalloonTipClicked
System.Diagnostics.Debug.WriteLine("BaloonTip が クリックされた時に発生")
End Sub
Private Sub NotifyIcon1_BalloonTipClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.BalloonTipClosed
System.Diagnostics.Debug.WriteLine("BaloonTip が 閉じられた時に発生")
End Sub
Private Sub NotifyIcon1_BalloonTipShown(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.BalloonTipShown
System.Diagnostics.Debug.WriteLine("BaloonTip が 表示された時に発生")
End Sub
End Class