隐藏程序并从show hidden图标重新打开它

时间:2014-09-16 13:44:46

标签: vb.net

当我关闭它时,我想隐藏我的程序在后台运行。当我点击通知图标时,我会再次显示该程序。提前致谢。 这是我的代码。

Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim response As MsgBoxResult
    playErrorSound()
    'Message that ask user to if his action means to close the application.
    response = MsgBox("Do you want to close?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
        'Stop timer 
        Timer1.Stop()
        Me.StopWatch.Stop()
        'close program
        Me.Hide()
        Me.ShowInTaskbar = True
    ElseIf response = MsgBoxResult.No Then
        'Keep form opened
        e.Cancel = True
        Exit Sub
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

使用NotifyIcon控件。

Public Class Form1
    Dim WithEvents notify As New NotifyIcon

    Sub New()
        InitializeComponent()
        Dim myIcon As New System.Drawing.Icon("c:\stuff\myicon.ico")
        notify.Icon = myIcon
        notify.Visible = False
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim response As MsgBoxResult
        playErrorSound()
        'Message that ask user to if his action means to close the application.
        response = MsgBox("Do you want to close?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
        If response = MsgBoxResult.Yes Then
            'Stop timer 
            Timer1.Stop()
            Me.StopWatch.Stop()
            'close program
            Me.Hide()
            Me.ShowInTaskbar = False
            notify.Visible = True
        End If 
        e.Cancel = True
    End Sub

    Private Sub notify_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles notify.DoubleClick
        Me.Show()
        Me.ShowInTaskbar = True
        notify.Visible = False
    End Sub
End Class