2013年1月20日 星期日

Use Notify icon in VB.NET

引用來源
--


Add two contextmenu item in contextMenuStrip1

1.Open(for showing application)

2.Exit (for closing application)

Now there is need to set some properties of notify icon like :

ContextMenu: Gets/Sets Context menu for the tray icon
Icon: Gets/Sets current icon
Text: Gets/Sets tooltip text that is displayed when the mouse hovers over the system tray
Visible: Gets/Sets if the icon is visible in the window system tray

On form closing event put this code, this code minimize the form

 Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

        If Me.WindowState = FormWindowState.Normal Then
            e.Cancel = True
            Me.Hide()
            Me.WindowState = FormWindowState.Minimized
        End If
    End Sub

And for showing form again we can use MouseDoubleClick event  of notify icon control like this:

Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
        Me.WindowState = FormWindowState.Normal
        Me.Activate()
        Me.Focus()
End Sub

 And it will show time on labels

  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = DateTime.Now.ToLongTimeString()
        Label2.Text = DateTime.Now.ToLongDateString()
  End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Label1.Text = DateTime.Now.ToShortTimeString()
        Label2.Text = DateTime.Now.ToLongDateString()
  End Sub

We can show tooltip on icon like Figure2 with the help of this code:

Private Sub NotifyIcon1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseMove
        NotifyIcon1.Text = DateTime.Now.ToLongTimeString
  End Sub

沒有留言:

張貼留言