- Imports System.ComponentModel
- Imports System.Drawing
- Imports System.WinForms
- Public Class Form1
- Inherits System.WinForms.Form
- 'TrayIcon icons
- Private mSmileIcon As Icon = New Icon("face02.ico")
- Private mFrownIcon As Icon = New Icon("face04.ico")
- Private mSmileDisplayed As Boolean
- Public Sub New()
- MyBase.New()
- Form1 = Me
- 'This call is required by the Win Form Designer.
- InitializeComponent()
- 'TODO: Add any initialization after the InitializeComponent() call
- 'this form isn't used directly so hide it immediately
- Me.Hide()
- 'setup the tray icon
- InitializeTrayIcon()
- End Sub
- Private Sub InitializeTrayIcon()
- 'setup the default icon
- TrayIcon = New System.WinForms.TrayIcon()
- TrayIcon.Icon = mSmileIcon
- TrayIcon.Text = "Right Click for the menu"
- TrayIcon.Visible = True
- mSmileDisplayed = True
- 'Insert all MenuItem objects into an array and add them to
- 'the context menu simultaneously
- Dim mnuItms(4) As MenuItem
- mnuItms(0) = New MenuItem("Show Form...", AddressOf Me.ShowFormSelect)
- mnuItms(0).DefaultItem = True
- mnuItms(1) = New MenuItem("Toggle Image", AddressOf Me.ToggleImageSelect)
- mnuItms(2) = New MenuItem("-")
- mnuItms(3) = New MenuItem("Exit", AddressOf Me.ExitSelect)
- Dim trayIconMnu As ContextMenu = New ContextMenu(mnuItms)
- TrayIcon.ContextMenu = trayIconMnu
- End Sub
- Public Sub ShowFormSelect(ByVal sender As Object, ByVal e As system.eventargs)
- 'Display the settings dialog
- Dim SettingsForm As New SettingsForm()
- SettingsForm.ShowDialog()
- End Sub
- Public Sub ToggleImageSelect(ByVal sender As Object, ByVal e As System.EventArgs)
- 'called when the user selects the 'Toggle Image' context menu
- 'determine which icon is currently visible and switch it
- If mSmileDisplayed Then
- 'called when the user selects the 'Show Form' context menu
- TrayIcon.Icon = mFrownIcon
- TrayIcon.Text = "Sad"
- mSmileDisplayed = False
- Else
- TrayIcon.Icon = mSmileIcon
- TrayIcon.Text = "Happy"
- mSmileDisplayed = True
- End If
- End Sub
- Public Sub ExitSelect(ByVal sender As Object, ByVal e As System.EventArgs)
- 'called when the user selects the 'Exit' context menu
- 'hide the tray icon
- TrayIcon.Visible = False
- 'close up
- Me.Close()
- End Sub
- 'Form overrides dispose to clean up the component list.
- Public Overrides Sub Dispose()
- MyBase.Dispose()
- components.Dispose()
- End Sub
- #Region " Windows Form Designer generated code "
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
- Private WithEvents TrayIcon As System.WinForms.TrayIcon
- Dim WithEvents Form1 As System.WinForms.Form
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- Private Sub InitializeComponent()
- Me.components = New System.ComponentModel.Container()
- Me.TrayIcon = New System.WinForms.TrayIcon()
- '@design Me.TrayHeight = 90
- '@design Me.TrayLargeIcon = False
- '@design Me.SnapToGrid = False
- '@design Me.TrayAutoArrange = True
- '@design TrayIcon.SetLocation(New System.Drawing.Point(7, 7))
- TrayIcon.Text = ""
- TrayIcon.Visible = True
- Me.MaximizeBox = False
- Me.StartPosition = System.WinForms.FormStartPosition.Manual
- Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
- Me.BorderStyle = System.WinForms.FormBorderStyle.None
- Me.Enabled = False
- Me.AccessibleRole = System.WinForms.AccessibleRoles.None
- Me.ShowInTaskbar = False
- Me.SizeGripStyle = System.WinForms.SizeGripStyle.Hide
- Me.ControlBox = False
- Me.MinimizeBox = False
- Me.ClientSize = New System.Drawing.Size(1, 7)
- Me.Opacity = 0#
- End Sub
- #End Region
- End Class
VB.NET系统托盘图标实例
最新推荐文章于 2016-06-14 14:07:22 发布