vb.net 如何制作一个只有通知图标而没有 Windows 窗体的应用程序?

发布于 2024-10-25 09:00:28 字数 86 浏览 0 评论 0原文

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

I want to make an application that only has a notifyicon and doesn't have any visible window form when it starts up. I see some example sort of like what I want to do for c#, but I don't see how to do this in vb.net project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-11-01 09:00:28

表格并不是绝对必要的。您可以实例化 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

A form is not strictly necessary. You can instantiate a NotifyIcon and use that without creating a form:

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

And then start your app from a Sub Main:

Public Module EntryPoint
    Public Sub Main()
        Dim ctx As New AppContext()
        Application.Run(ctx)
    End Sub
End Module
嘿咻 2024-11-01 09:00:28

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

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

NotifyIcon.Visible = True

然后进行您想要的操作..

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

希望它对你有帮助..

Just put the form transparent and resize it to 1x1..
And add a notifyicon..

And on the Form Load Event do this:

NotifyIcon.Visible = True

Then make what ever you want..

You can create a context menu strip (A Menu when you right click on it )
PS: If you do it, You need to go on the NotifyIcon properties and set the Context Menu Strip to that you created..

Hope it helped you..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文