vb.net如何创建一个只有一个notifyicon而没有windows窗体的应用程序?

时间:2011-03-21 18:34:58

标签: vb.net notifyicon

我想创建一个只有一个notifyicon但在启动时没有任何可见窗口形式的应用程序。我看到一些例子就像我想为c#做的那样,但我不知道如何在vb.net项目中做到这一点。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

只需将表单透明并将其调整为1x1 .. 并添加一个notifyicon ..

在表单加载事件上执行此操作:

NotifyIcon.Visible = True

然后制作你想要的东西..

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

希望它对你有所帮助..