2

我想制作一个只有一个通知图标并且在启动时没有任何可见窗口窗体的应用程序。我看到了一些类似于我想为 c# 做的示例,但我看不到如何在 vb.net 项目中执行此操作。

4

2 回答 2

6

表格不是绝对必要的。您可以实例化 NotifyIcon 并在不创建表单的情况下使用它:

Public Class AppContext
    Inherits ApplicationContext

    Private notifyIcon As NotifyIcon
    Private appActive As Boolean

    Public Sub New()
        AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

        notifyIcon = New NotifyIcon()
        notifyIcon.Icon = My.Resources.ActiveIcon
        notifyIcon.Text = "The app is active."

        AddHandler notifyIcon.MouseClick, AddressOf OnIconMouseClick

        appActive = True
        notifyIcon.Visible = True

    End Sub

    Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
        If notifyIcon IsNot Nothing Then
            notifyIcon.Dispose()
        End If
    End Sub

    Private Sub OnIconMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)

        If e.Button = MouseButtons.Left Then
            appActive = Not appActive
            notifyIcon.Icon = If(appActive, My.Resources.ActiveIcon, My.Resources.InactiveIcon)
            notifyIcon.Text = If(appActive, "The app is active.", "The app is not active.")
        Else
            If MsgBox("Do you want to Exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                notifyIcon.Visible = False
                ExitThread()
            End If
        End If
    End Sub

End Class

然后从 Sub Main 启动您的应用程序:

Public Module EntryPoint
    Public Sub Main()
        Dim ctx As New AppContext()
        Application.Run(ctx)
    End Sub
End Module
于 2011-03-22T15:10:14.380 回答
0

只需将表单透明并将其调整为 1x1 .. 并添加一个通知图标 ..

在表单加载事件中执行以下操作:

NotifyIcon.Visible = True

然后做任何你想要的..

您可以创建一个上下文菜单条(右键单击它时的菜单) PS:如果这样做,您需要继续 NotifyIcon 属性并将上下文菜单条设置为您创建的..

希望对你有帮助。。

于 2011-03-21T18:57:15.983 回答