[VB.NET][C#.NET] 如何使用 系統閘 動畫 / System tray animate / 應用程式 顯示在 右下角
1.在VB6時就曾經玩過這種東西,那時費了好大的功夫才完成,一大堆的API組成,光想到就無力了,然而現在.Net提供了如此方便的東西怎可輕易放過它。
2.現在只要用 NotifyIcon 及 contextMenuStrip 控制項就能搞定
3.搭配 時間人 達到j輪撥圖片的功能,不知啥小是 時間人 的請到 時間人 參考;若要使用時間人運算精準的東西,建議使用System.Threading.Timer,下列範例則是採用System.Windows.Forms.Timer 控制項。
4.下列範例圖示是借用 Comodo Firewall 應用程式來做說明,若要利用該圖形進行商業化套裝,請考慮圖形版權。
設定步驟如下
1.拖曳一個 NotifyIcon 及 contextMenuStrip 兩個 Timer 控制項至專案
2.設定 notifyIcon1.Visible = False,設定 notifyIcon1.ContexMenuStrip= contextMenuStrip
3.定義notifyIcon1的Icon屬性,timer1將每0.2秒變換一個圖形,最後一張圖停留5秒;再由timer2啟動timer1,週而復始。
'時間人1:建立系統閘動畫
Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
Select Case i
Case 37
notifyIcon1.Icon = New Icon("icon\" + i.ToString() & ".ico")
timer1.Enabled = False
timer2.Enabled = True
timer2.Interval = 5000
Exit Select
Case 38
i = 1
timer1.Interval = 200
Exit Select
Case Else
timer1.Interval = 200
CretaeIcon(i)
Exit Select
End Select
i += 1
End Sub
'建立系統閘動畫
Private Sub CretaeIcon(ByVal i As Integer)
notifyIcon1.Icon = New Icon("icon\" + i.ToString() & ".ico")
End Sub
'時間人2:時間到了叫時間人1起床
Private Sub timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer2.Tick
timer1.Enabled = True
timer1.Interval = 200
timer2.Enabled = False
End Sub
'視窗作小化時縮至系統閘
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'若將視窗最小化
If Me.WindowState = FormWindowState.Minimized Then
notifyIcon1.Visible = True
Hide()
timer1.Enabled = True
timer1.Interval = 200
notifyIcon1.Visible = True
Else
'若不是最小化
Me.notifyIcon1.Visible = False
End If
End Sub
5.設定 contextMenuStrip1 子選單,依個人需求增加子選單數量
點選”在這裡輸入”旁邊的小箭頭,選擇MenuItem
並設定Text屬性
6.設定System tray的滑鼠操作事件,利用notifyIcon1的MouseDown事件,判斷滑鼠是否使用左鍵按下,而右鍵已經交由contextMenuStrip1選單處理了,只要設定子選單的Click事件的設定即可。
#Region "對 System Tray 按左鍵功能選單"
'對 System Tray 按左鍵視窗正常化
Private Sub notifyIcon1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles notifyIcon1.MouseDown
If e.Button = MouseButtons.Left Then
Me.Show()
Me.WindowState = FormWindowState.Normal
End If
End Sub
#End Region
#Region "對 System Tray 按右鍵功能選單"
'關閉應用程式
Private Sub toolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem2.Click
Me.Dispose()
End Sub
'最大化
Private Sub toolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem1.Click
Me.Show()
Me.WindowState = FormWindowState.Maximized
Me.notifyIcon1.Visible = False
End Sub
#End Region
8.範例下載:
9.實作畫面
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET