我将应用程序编码为隐藏并在系统托盘中显示图标。当您单击图标并退出时,我的应用程序将消失,但应用程序仍显示在任务管理器中运行。这是我的结束:
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
End sub
Private Sub OpenWorkSheet_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Visible = False
Me.NotifyIcon1.Visible = True
End Sub
Private Sub OpenToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem1.Click
Try
NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
Me.Visible = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Visible = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
Me.Close()
Application.Exit()
Me.Dispose()
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Try
NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
Me.Visible = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
声明一个布尔变量,指示您是否关闭整个应用程序,以便您可以正确关闭表单。
如果您不选择关闭整个应用程序,则只会将e.Cancel
设置为True。
Dim ClosingApp As Boolean = False
Private Sub OpenWorkSheet_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If ClosingApp = False Then
e.Cancel = True
Me.Visible = False
Me.NotifyIcon1.Visible = True
End If
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
ClosingApp = True
NotifyIcon1.Visible = False
Me.Close()
Application.Exit()
End Sub
我还删除了一些多余的行,例如处理表格不是必需的 A)您已经关闭了应用程序,它的所有已用内存都将被释放。 B)关闭表单会自动处理它。